Giter Club home page Giter Club logo

docker-play-environment's Introduction

WORKING WITH CONTAINERS

Pre-requisite - Setting up virtual machine using Virtual Box and Vagrant


  1. Install Virtual Box from the below given link -

Download Virtual Box 6.1.X!

  1. Install Vagrant from the below given link -

Download Vagrant!

Restart your machine.

  1. Install Visual Studio Code (you can use any other text editor) -

Download Visual Studio Code!

  1. Create a new Data directory and clone the below repository inside that directory.
git clone https://github.com/sapanachhetri/docker-play-environment.git

NOTE: Install git from here if you do not have it.

  1. Once the repository is cloned change the directory to setup-files using the command -
cd docker-play-environment
  1. Bring up the virtual machine by typing in below command -
vagrant up
  1. To log into the virtual machine use the below command -
vagrant ssh
  1. Check the docker version by executing the command without sudo -
docker version
  1. To display the system-wide information use the below command -
docker info
  1. To stop the running virtual machine i.e. part of Vagrantfile first exit the session then you can use the command
vagrant halt
  1. To start your virtual machine again use the below command –
vagrant up

and then ssh to this machine using the command –

vagrant ssh
To access the virtual machine using putty -
Once vagrant up is working it creates .vagrant>machines>minion1>virtualbox> directory
convert the Private_key to a ppk file using puttygen
In putty use the IP address from .vagrant file and use the ppk file generated to connect to the VM.

Reference Link - Installing Docker Engine On Ubuntu

Reference Link - Installing Docker Engine On CentOS

Reference Link - Installing Docker Engine On Fedora

  1. Visit the below link and sign up for a new account if you don’t have one -

Docker Hub

  1. Check for the confirmation email which you have used to sign up for docker hub and activate your account by verifying your email address.

Module - Docker Basics


Activity 1 - Searching, Pulling, and Displaying local images


  1. Log in to the Docker Hub account, explore different docker images by typing image name in the search explorer. Try image like: java, python, nginx, alpine, accenture/adop-nginx etc.

  2. To list the different flags available with search use the below command -

docker search --help
  1. Use the below command to search for an image using the docker cli –
# docker search <term>
docker search adop
  1. Use the different flags with search command like:

i. The --no-trunc flag is used to display a non-truncated description.

ii. The --limit flag is used to limit the search results. It should be in range of 1 – 100 and default value is 25.

iii. The --format flag is used to pretty print the table using the Go template.

iv. The --filter flag is used to filter the search result. The currently supported filters are stars, is-official and is-automated.

# Example 1
docker search --no-trunc --limit 10 adop
# Example 2
docker search --format "table {{.Name}}\t{{.IsAutomated}}\t{{.IsOfficial}}\t{{.StarCount}}" adop
# Example 3
docker search --filter stars=6 --filter is-automated=true --limit 10 adop

REFERENCE LINK - OUTPUT FORMATTING

  1. To list all sub-commands available with the docker image management command use -
docker image --help
  1. To pull an image from a registry (default is hub.docker.com) use –
# New Way
# docker image pull <IMAGE-NAME>[:<TAG>]
docker image pull alpine

# Old Way
# docker pull <IMAGE-NAME>[:<TAG>]
docker pull busybox:1.28.1

NOTE: If tag is not specified, default is always latest.

  1. To display list of all local images use -
# New Way
docker image ls

# Old Way
docker images

Activity 02 - Executing an image and listing containers


  1. To list all sub-commands available with the docker container management command use –
docker container --help
  1. To view all options available to work with run use the below command -
docker container run --help
  1. To execute a simple docker image use the below command –
# New Way
docker container run hello-world
# Old Way
docker run hello-world

NOTE: The hello-world image is not pulled again when the command is executed second time because it is cached locally.

  1. To create a container from an existing image (i.e. alpine) and execute a command inside it use -
docker container run alpine echo "Hello, World!"

NOTE: After the command execution, the container exits irrespective of the outcome.

  1. To list the directory of alpine image by running ls command inside a container use -
docker container run alpine ls
  1. To list the processes running inside container of alpine image use ps command with arguments -ef -
docker container run alpine ps -ef

NOTE: Ideally, there should only be a single process running inside a container.

  1. To list all available options with the container ls sub-command use –
docker container ls --help
  1. To list all running containers use –
# New Way
docker container ls

# Old Way
docker ps 
  1. To list all running and stopped containers use the option -a or --all with ls
# New Way
docker container ls -a

# Old Way
docker ps -a

NOTE: When you run a docker container without using the --name option, the docker engine assigns a name to that container. The name is comprised of an adjective and a famous personality last-name, joint by an underscore (_).

  1. Try out the below commands with different options.
# display all the containers including exited one
docker container ls -a 
# display only the containers id including exited one
docker container ls -aq
# display the last container started 
docker container ls -l 
# display only the id of last container started
docker container ls -lq 
  1. The --filter option can be used to filter out the containers based on a key=value –
# docker container ls --filter key=value
docker container ls --filter status=exited

REFERENCE LINK - docker container ls

SUPPORTED FILTERS


Activity 03 - Using a container with terminal


  1. To use a container with terminal requires two different options –

i. -i, --interactive - It keeps STDIN open even if not attached.

ii. -t, --tty - Allocates a pseudo-TTY

  1. To open an interactive pseudo terminal use -
# New Way
docker container run --interactive --tty alpine sh
docker container run -i -t alpine sh
docker container run -it alpine sh

# Old Way
docker run --interactive --tty alpine sh
docker run -i -t alpine sh
docker run -it alpine sh
  • Add a new file by using touch command

  • Then list down the directory and files inside the container using ls command

  • Type exit to shutdown and exit the terminal.

NOTE: When the terminal exits, then the container is shut down.

docker container ls -al
  1. The new container that uses the same image will run with its thin writeable layer. Therefore, the file saved earlier is not present. Try it out!
docker container run -it alpine sh

# on the container shell type ls to list directories and files 
ls

NOTE: To keep the container running rather than shut it down, use the keys combination of (ctrl + p + q).

  1. To exec into container shell once again use -
# New Way
docker container exec -it <CONTAINER-ID|NAME> sh
# Old Way
docker exec -it <CONTAINER-ID|NAME> sh

NOTE: Typing the command exit does not terminates the container if its shell is accessed using exec; therefore, there is no need to use the keys combination of ctrl + p + q.


Activity 04 - Running container in detached mode


  1. The information or logs of the container gets displayed directly on the host client, when the container ran without using the --detach or -d option.
docker container run alpine ping localhost -c 4
  1. The --detach or -d option does not log the container information, and it runs the container in detached mode.
docker container run --detach alpine ping localhost -c 100
# OR
docker container run -d alpine ping localhost -c 100
  1. To see the list of running containers use -
docker container ls
  1. To attach the client back to the docker container use the attach command –
# New Way
docker container attach <CONTAINER-ID|NAME>

# Old Way
docker attach <CONTAINER-ID|NAME>

Reference link - docker container attach


Activity 05 - Running a web server application container


  1. To pull the nginx web server image use -
docker image pull nginx:alpine
  1. The --name option assigns a name to the container, instead of default name given by the docker engine.

  2. The -P (capital P) option publishes the container port, which are exposed, to a random port on the host.

  3. To run the nginx webserver on host machine published on random port in detached mode use -

docker container run --detach --name webserver -P nginx:alpine
  1. To view the published port use the container ls command -
docker container ls
  1. Open a new web browser tab window, and type the URL as given below.
http://<HOST-IP-ADDRESS>:<PUBLISHED-PORT>

NOTE: HOST-IP-ADDRESS is specified in the Vagrantfile.

  1. The --publish or -p option binds the container ports that are exposed to specific ports on the host machine.
# <HOST-PORT>:<CONTAINER-PORT>
docker container run --detach --name webserver2 --publish 80:80 nginx:alpine
# OR
docker container run -d --name webserver2 -p 80:80 nginx:alpine

NOTE: The above command binds port 80 of the container to TCP port 80 of the host machine. The default protocol is TCP unless specified otherwise.

  1. We can run more nginx containers with different names and its ports published on different host ports that are vacant.
docker container run -d --name webserver3 -p 8888:80 nginx:alpine
  1. To list down the running containers use -
docker container ls
  1. Open multiple new web browser tab window, and access the different web server instance running on respective host ports.

Activity 06 - Debugging a container application


  1. To view all available options of the container logs management command use –
docker container logs --help
  1. To fetch the container logs use –
# New Way
# docker container logs [OPTIONS] <CONTAINER>
docker container logs webserver

# Old Way
# docker logs [OPTIONS] <CONTAINER>
docker logs webserver
  1. The -f or --follow option with logs command helps to follow log output.
docker container logs --follow webserver

NOTE: If we refresh the home page of the webserver instance, it outputs new log entries.

  1. The --tail option displays the container logs information from the end depending on number passed as an argument.
docker container logs --tail 2 webserver

NOTE: A negative number or a non-integer, when passed as an argument to --tail option is invalid. The value is set to default all in that case.

  1. To list all the available options of container inspect command use –
docker container inspect --help
  1. To inspect all available states of the container use –
# New Way
# docker container inspect <CONTAINER> [CONTAINER …]
docker container inspect webserver

# Old Way
# docker inspect <CONTAINER> [CONTAINER …]
docker inspect webserver
  1. To get more specific details of the container, use the --format option.
docker container inspect --format "{{.Config.Image}}" webserver
  1. To return sub-section of the json use the below command -
docker container inspect --format "{{json .Config}}" webserver
  1. Try the grep command use –
docker container inspect webserver | grep -i Image

Activity 07 - Stopping, Starting, Killing, and Removing container


  1. For a graceful shutting down of container use the stop command -
# New Way
# docker container stop <CONTAINER>
docker container stop webserver

# Old Way
# docker stop <CONTAINER>
docker stop webserver2
  1. We can also use the kill command to stop a running container but it will stop the main entry point process abruptly which may lead to a damaged file system.
# New Way
# docker container kill <CONTAINER>
docker container kill webserver3

# Old Way
# docker kill <CONTAINER>
  1. For starting a stopped container use the command –
# New Way
# docker container start <CONTAINER>
docker container start webserver

# Old Way
# docker start <CONTAINER>
docker start webserver2
  1. To start a container and attach it to the client use the -a option with start command
docker container start -a webserver3

You can open a new web browser tab and use the IP address along with container port number which is 8888 in our case. That will log the output.

NOTE: Use (ctrl + c) to stop the running container in attach mode.

  1. To filter out exited container use --filter option -
docker container ls --filter status=exited
  1. To remove a stopped container without any error using the command
# New Way
# docker container rm <CONTAINER>
docker container rm webserver3

# Old Way
# docker rm <CONTAINER>
docker rm <CONTAINER>
  1. To remove a running container use the -f or --force option -
docker container rm --force <CONTAINER_ID|CONTAINER_NAME> 
  1. To pass the output of one docker command to another docker command use the $ symbol. To remove all the containers running or stopped use -
docker container rm -f $(docker container ls -aq)
  1. Use the --rm option when creating a new container, which will automatically remove the container when it is stopped.
docker container run --detach --rm --name webserver --publish 80:80 nginx:alpine
  1. To stop the container created earlier and verify use -
docker container stop webserver 
# verify after the container is stopped, it is removed because of --rm option used while running a container
docker container ls -a

Activity 08 - Setting environment variables


  1. To set an environment variables that would be used by container use the --env or -e option.
docker container run --rm \
--env SOME_VAR=ValueWithoutSpaces \
-it alpine sh
  1. To set multiple environment variables you can use --env or -e option multiple times in command -
docker container run --rm \
--env SOME_VAR_ONE=ValueOne \
--env SOME_VAR_TWO="Value With Spaces" \
--env SOME_VAR_THREE=ValueThree \
-it alpine sh
  1. To echo the different variables value set inside container use -
echo $SOME_VAR_ONE && echo $SOME_VAR_TWO && echo $SOME_VAR_THREE

NOTE: The syntax of && is used to join multiple commands.


Activity 09 - Bind Mounting a container


  1. Create a my-web-app directory and then create a simple HTML file with name as index.html.
mkdir ~/my-web-app
vim ~/my-web-app/index.html

index.html content below -

<html>
    <body>
        <h1>ATCI : Working with Containers!</h1>
    </body>
</html>
  1. The --volume or -v option is used to bind mount the app directory to web server application.
--volume <HOST-PATH> : <CONTAINER-PATH> : [<OTHER-OPTIONS>]
docker container run --rm --detach --name webserver --publish 80:80 \
--volume $(pwd)/my-web-app:/usr/share/nginx/html:ro \
nginx:alpine

NOTE: we have used ro for readonly purpose

  1. Open a new web browser tab and type the IP address on the address bar to see the content of your index.html file that gets mounted inside container html directory.

  2. Starting from docker 17.06 a new option i.e. --mount is preferred over --volume option.

--mount type=(bind|volume|tmpfs),source=HOST-PATH,destination=CONTAINER-PATH,[options]
OR
--mount type=(bind|volume|tmpfs),source=HOST-PATH,target=CONTAINER-PATH,[options]
OR
--mount type=(bind|volume|tmpfs),src=HOST-PATH,dst=CONTAINER-PATH,[options]
docker container run --rm --detach --name anotherwebserver --publish 8888:80 \
--mount type=bind,src=$(pwd)/my-web-app,dst=/usr/share/nginx/html,readonly \
nginx:alpine
  1. Open a new web browser tab and type the IP address on the address bar, use the port number 8888 to access the modified index.html page.

  2. Try updating the file in my-web-app directory and refresh the browser window to see modified changes.

  3. To remove all the containers use -

docker container rm -f $(docker container ls -aq)

Activity 10 - Volume mounting a container


  1. To see a host of available sub commands of volume management command use -
docker volume --help
  1. To create a new volume managed by docker use -
# docker volume create <VOLUME>
docker volume create my-alpine-volume
  1. To list the volume created use the command –
docker volume ls
  1. To inspect the volume use the command -
# docker volume inspect <VOLUME>
docker volume inspect my-alpine-volume
  1. To add this volume to a container use the below command –

To mount the volume my-alpine-volume into /custom_directory in the container that could be access using the exec command.

docker container run --name alpine-one -it \
--mount source=my-alpine-volume,target=/custom_directory \
alpine sh

NOTE: The custom_directory is created automatically inside the container to mount the volume.

  1. To add a new file inside the custom_directory use touch command -
touch custom_directory/sample.txt
  1. To list the content of custom_directory use the command below, then exit using (ctrl + p + q) -
ls -al custom_directory
  1. Create another container that would be mounted on the same volume and we can see the same file inside this new container.
docker container run --name alpine-two -it \
--mount source=my-alpine-volume,target=/my_directory \
alpine sh
  1. To view the file created in alpine-one container inside my_directory use -
ls -al my_directory
  1. Add a new file from this running container, say another_sample.txt inside the my_directory
touch my_directory/another_sample.txt
  1. To come out of the container and keep it running use (ctrl + p + q)

  2. Now, exec into the alpine-one container and check the content of the custom_directory

docker container exec -it alpine-one sh
ls -al custom_directory
  1. To remove a docker volume use the below command -
# docker volume rm <volume-name>
docker volume rm my-alpine-volume

NOTE: If you try to remove the volume which is already attached to the container it will throw the error.

  1. First remove all the containers attached to the volume we have to remove –
docker container rm -f $(docker container ls -aq)
docker volume rm my-alpine-volume

NOTE: If a container is started with a volume that doesn't exist, docker will automatically create that volume.


Activity 11 - Creating a network and assigning it to an existing container


  1. To create two containers communicating with each other use -
docker container run --detach --rm --name alpine-one alpine sleep 3600
docker container run --detach --rm --name alpine-two alpine sleep 3600
  1. To check the status of the container running use -
docker container ls
  1. To ping from alpine-one container to alpine-two container just by using container name use -
docker container exec -it alpine-one ping -c 4 alpine-two
  1. To see the list of available sub-command use with network management command use -
docker network --help
  1. To create a new network using the bridge driver use the below command -
# docker network create --driver bridge <NETWORK-NAME>
docker network create --driver bridge my-network
  1. To see the list of available networks use -
docker network ls
  1. To connect the existing container to a network use -
docker network connect my-network alpine-one
docker network connect my-network alpine-two
  1. To inspect a network use –
# docker network inspect <NETWORK-NAME>
docker network inspect my-network

NOTE: Check that both the containers are connected to this network

  1. Now, try to ping from alpine-one container to alpine-two container once again -
docker container exec -it alpine-one ping -c 4 alpine-two
  1. To disconnect an existing container from a network use disconnect command -
docker network disconnect my-network alpine-one
docker network disconnect my-network alpine-two
docker container inspect my-network
  1. To remove a network use the command -
# docker network rm <NETWORK-NAME>
docker network rm my-network
  1. To delete the containers created use -
docker container rm -f $(docker container ls -aq)

Case Study - Creating a MySQL server and connecting to it using Adminer (a web based UI to communicate with MySQL Server)


In this case study, we need to set up a new mysql server and using a web ui we should be able to execute sql query on the mysql server.

HINTS:

  1. Look for the official MySQL image on hub.docker.com. Read the description to understand the different environment variables.

  2. To store the data generated by mysql create a volume and mount it to container.

  3. Look for the official adminer image on hub.docker.com. Read the description to understand the different environment variables. Look for the specific environment variables to set the name of mysql server.

  4. Create a separate network to join the adminer and mysql server for communication.


Module - Managing and Creating Images


Activity 12 - Commit Changes in a Running Container


  1. To run a nginx webserver inside a container use -
docker container run --rm --detach --name webserver -p 80:80 nginx:alpine
  1. Open a web browser and access the default web page for nginx webserver, using the private IP address

  2. To exec into the webserver container use -

docker container exec -it webserver sh
  1. Open the index.html file, which is located in the path given below -
vi /usr/share/nginx/html/index.html

and edit it like

<body>
    <h1>ATCI - Working with Containers Training!</h1>
</body>

NOTE:

use the key I to be in insert mode to edit the file

use esc with :wq to write and quit from vi

  1. After changes, to exit out of the shell use the exit command. Refresh the browser tab to see the modified changes.

  2. To list all the options available with commit command use -

docker container commit --help
  1. To commit changes, and create a new image from a container use:
# New Way
# docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
docker container commit --message "My Nginx Image!" webserver mynginx:1.0

# Old Way
# docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
  1. To view the image that is just created use -
docker image ls
  1. To stop the container use -
docker container stop webserver

Activity 13 - Build Image Using Dockerfile


  1. To view the different options available with the build command use -
docker image build --help
  1. Create a new directory say my-alpine-repo and add a new Dockerfile file inside that directory.
mkdir ~/my-alpine-repo && cd ~/my-alpine-repo
vim ~/my-alpine-repo/Dockerfile
  1. Add the below content to the Dockerfile
FROM alpine
RUN apk upgrade
RUN apk add vim
RUN apk add tree
  1. To build an image from Dockerfile use -
# New Way
# docker image build [OPTIONS] PATH | URL | -
docker image build -t my-alpine:1.0 .

# Old Way
# docker build [OPTIONS] PATH | URL | -
  1. To run a container in interactive mode from the recently created image use -
docker container run --rm -it --name alpine-demo my-alpine:1.0 sh
  1. Use the tree command to view the directory structure of bin –
tree bin
  1. To exit out of the container shell use exit command.

  2. To create a new file Dockerfile-2 from existing Dockerfile inside my-alpine-repo directory use -

cp ~/my-alpine-repo/Dockerfile ~/my-alpine-repo/Dockerfile-2
  1. Modify the content of Dockerfile-2 as shown below -
FROM alpine
RUN apk upgrade
RUN apk add vim && apk add tree
  1. To create the image use --file or -f option to specify the path of Dockerfile, if it is different than default file name, which is Dockerfile -
docker image build -f Dockerfile-2 -t my-alpine:2.0 .

NOTE: To build this image, the intermediate cache image layer is used from previous image build. Remember, both the images are build in the same host machine.

  1. To avoid using cache for re-building an image, use the --no-cache option -
docker image build --no-cache -f Dockerfile-2 -t my-alpine:3.0 .

NOTE: The RUN apk upgrade step has executed again and no cache image layer was used.

  1. To view list of options available with history command use -
docker image history --help
  1. To view the history of an image use:
# New Way
# docker image history [OPTIONS] IMAGE
docker image history alpine

# Old Way
# docker history [OPTIONS] IMAGE
  1. Observe, some similar image layer history for different images as it was build using the cache image layer.
docker image history my-alpine:1.0
docker image history my-alpine:2.0
docker image history my-alpine:3.0

NOTE: When a image is pushed to the registry only the leaf image i.e. the last layer is uploaded along with its constituent layer that makes up an entire image. When this image is made available and pulled again by another docker host the components that supports build cache are no longer required as the image is effectively read-only and hence the <missing> for image id.


Activity 14 - Using CMD


  1. Create a new file say Dockerfile-3 from the existing Dockerfile-2
cp ~/my-alpine-repo/Dockerfile-2 ~/my-alpine-repo/Dockerfile-3
  1. Modify the content of Dockerfile-3 as shown below -
FROM alpine
RUN apk upgrade
RUN apk add vim && apk add tree
CMD ping -c 4 localhost

NOTE: The CMD instruction has three forms:

  1. CMD ["executable","param1","param2"] (exec form, this is the preferred form)

  2. CMD ["param1","param2"] (as default parameters to ENTRYPOINT)

  3. CMD command param1 param2 (shell form)

NOTE: There can only be one CMD instruction in a Dockerfile. If you list more than one CMD, then only the last CMD will take effect.

  1. To build the image using Dockerfile-3 use -
docker image build -f Dockerfile-3 -t my-alpine:cmd-shell-form .
  1. To run a new container from my-alpine:cmd-shell-form image that exist locally use -
docker container run --rm --name ping-container my-alpine:cmd-shell-form
  1. If any command is specified after the image name, it will override the CMD specified inside the image.
docker container run --rm --name ping-container my-alpine:cmd-shell-form echo "Docker is AWESOME"
  1. Open the Dockerfile-3 and change the CMD to use the exec form as shown below
FROM alpine
RUN apk upgrade
RUN apk add vim && apk add tree
CMD ["ping", "-c", "4", "localhost"]

NOTE: Use double quotes to put the executable and parameters

  1. To create the image again use -
docker image build -f Dockerfile-3 -t alpine:cmd-exec-form .
  1. To run a new container from my-alpine:cmd-exec-form image that exist locally use -
docker container run --rm --name ping-container alpine:cmd-exec-form

Activity 15 - Using ENTRYPOINT


  1. Create a new file say Dockerfile-4 from the existing Dockerfile-3
cp ~/my-alpine-repo/Dockerfile-3 ~/my-alpine-repo/Dockerfile-4
  1. Modify the content of Dockerfile-4 as shown below -
FROM alpine
RUN apk upgrade
RUN apk add vim && apk add tree
ENTRYPOINT ping -c 4 localhost

NOTE: ENTRYPOINT has two forms:

ENTRYPOINT command param1 param2 (shell form)

ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)

  1. To build the image using Dockerfile-4 use -
docker image build -f Dockerfile-4 -t my-alpine:ep-shell-form .
  1. To run a new executable container from my-alpine:ep-shell-form image that exist locally use -
docker container run --rm --name ping-container my-alpine:ep-shell-form
  1. Open the Dockerfile-4 and change the CMD to use the exec form as shown below
FROM alpine
RUN apk upgrade
RUN apk add vim && apk add tree
ENTRYPOINT ["ping", "-c", "4", "localhost"]
  1. To create the image again use -
docker image build -f Dockerfile-4 -t alpine:ep-exec-form .
  1. To run a new container from my-alpine:ep-exec-form image that exist locally use -
docker container run --rm --name ping-container alpine:ep-exec-form

NOTE: If you have created an image using ENTRYPOINT, then any command passed to the container after the image name, will be considered as a parameter for the executable. Hence, we will get an error.


Activity 16 - Using CMD and ENTRYPOINT together


  1. Create a new file say Dockerfile-5 from the existing Dockerfile-4
cp ~/my-alpine-repo/Dockerfile-4 ~/my-alpine-repo/Dockerfile-5
  1. Modify the content of Dockerfile-5 as shown below -
FROM alpine
RUN apk upgrade
RUN apk add vim && apk add tree
ENTRYPOINT ["ping", "localhost", "-c"]
CMD ["4"]

NOTE: ENTRYPOINT allows to configure a container to run as an executable with ping command, and then pass the number of pings as parameter through CMD.

  1. To build the image using Dockerfile-5 use -
docker image build -f Dockerfile-5 -t my-alpine:cmd-ep-exec-form .
  1. To run a new executable container from my-alpine:cmd-ep-exec-form image that exist locally use -
docker container run --rm --name ping-container my-alpine:cmd-ep-exec-form
  1. To run same container with more number of pings, pass additional parameter as integer value which in turn will replace the default CMD value of 4 as expected -
docker container run --rm --name ping-container my-alpine:cmd-ep-exec-form 10

REFERENCE LINK - HOW CMD AND ENTRYPOINT INTERACT


Activity 17 - Using WORKDIR and ADD


  1. Create a new directory say my-java-repo with an app directory inside it.
mkdir -p ~/my-java-repo/app && cd ~/my-java-repo 
  1. Create a new Greeting.java file, and add the content given below -
vim ~/my-java-repo/app/Greeting.java
public class Greeting {
    public static void main(String [] args) {
        System.out.println("Welcome from Containerized Java App!");
    }
}
  1. Create a new Dockerfile file inside my-java-repo, and add the content given below -
vim ~/my-java-repo/Dockerfile
FROM openjdk:8u181-jdk-slim
WORKDIR java-apps
COPY app/Greeting.java ./
RUN javac Greeting.java && rm -rf Greeting.java
ENTRYPOINT ["java", "Greeting"]
  1. To build an image from this Dockerfile use -
docker image build -t my-java-app:1.0 .
  1. To run a container using my-java-app:1.0 image, which exists locally now, use -
docker container run --rm --name java-app my-java-app:1.0

NOTE: The message is printed on the console and container terminates.


Activity 18 - Using Multi-Stage Build


  1. Create a new file say Dockerfile-2 from the existing Dockerfile
cp ~/my-java-repo/Dockerfile ~/my-java-repo/Dockerfile-2 
  1. Modify the content of Dockerfile-2 as given below -
# STAGE 1 - To build the Java Application
FROM openjdk:8u181-jdk-slim AS build_container
WORKDIR java-apps
COPY app/Greeting.java ./
RUN javac Greeting.java && rm -rf Greeting.java

# STAGE 2 - To package the Java Application
FROM openjdk:8u181-jre-slim
WORKDIR app
COPY --from=build_container java-apps/ ./
ENTRYPOINT ["java", "Greeting"]
  1. To build an image from this Dockerfile-2 use -
docker image build -f Dockerfile-2 -t my-java-app:2.0 .
  1. To run a container from the image my-java-app:2.0, which exists locally now, use -
docker container run --rm --name java-app my-java-app:2.0
  1. To check the size of different version of my-java-app images use -
docker image ls | grep -i my-java-app

REFERENCE LINK - Dockerfile


Activity 19 - Pushing an Image to Public Registry


  1. To install git in this node instance use -
# for ubuntu machine use
sudo apt-get install -y git

# for centos machine use
sudo yum install -y git
  1. Once git is installed, then clone the repository.
git clone https://github.com/sapanachhetri/docker-play-environment.git 
  1. Move to the lab-files/python-flask-app directory inside the docker-play-environment directory.
cd ~/docker-play-environment/lab-files/python-flask-app
  1. Refer the files in python-flask-app directory.
ls -al
  1. To open the Dockerfile and modify its configuration use -
vim Dockerfile
  1. To build a new docker image use -
# Don't forget to use the . at the end
docker image build -t python-flask-app:1.0 .
  1. To see the docker image created just now use -
docker image ls
  1. To run a new container from the docker image created use -
docker container run --detach --rm --name py-web-app-1 --publish 8888:80 python-flask-app:1.0
  1. Open a new browser tab window and use the URL given below to access the application.
http://<HOST-IP-ADDRESS>:8888
  1. To stop the running container use -
docker container stop py-web-app-1
  1. To log in to your account at the docker hub registry use -
docker login

NOTE: Use your docker hub id and password.

  1. To push the image first tag it with repository name (i.e. docker hub id).
docker image tag python-flask-app:1.0 <docker-hub-id>/python-flask-app:1.0
docker image tag python-flask-app:1.0 <docker-hub-id>/python-flask-app:latest

NOTE: The image that is pushed or pulled has the format domain/repository/image:tag. If not specified, the domain defaults to docker.io, the repository defaults to library, and the tag defaults to latest.

  1. To view all the repository images and its tags available locally use -
docker image ls | grep -i python
  1. To push the image to the docker hub registry use -
docker push <docker-hub-id>/python-flask-app:1.0
docker push <docker-hub-id>/python-flask-app:latest
  1. Once the image is pushed, you can view the image in your docker hub registered account.

  2. To remove the python-flask-app images from the host machine use -

docker image rm python-flask-app:1.0 <docker-hub-id>/python-flask-app:latest <docker-hub-id>/python-flask-app:1.0
  1. To use the pushed image from docker hub registry use -
docker container run --detach --rm --name py-web-app-1 --publish 8888:80 <docker-hub-id>/python-flask-app
  1. To view the application open a new browser tab window, and use -
http://<HOST-IP-ADDRESS>:8888
  1. To stop the running container use -
docker container stop py-web-app-1

END OF COURSE


docker-play-environment's People

Contributors

sapanachhetri avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.