How are you all doing? Hope you are enjoying what you do, like me 🙂
These days I am digging down docker a bit more and think that I need to keep the story here for further reference. I hope you already gone through my previous article How to install Docker on Ubuntu 18.04
Now its time to use docker for simple tasks.
Docker command takes a chain of options, commands followed by arguments. The syntax takes this form:
docker [option] [command] [arguments]
To view all available subcommands, type:
As of Docker 19, the complete list of available subcommands includes:
Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes
To view the options available to a specific command, type:
docker docker-subcommand --help
To view system-wide information about Docker, use:
Let’s have a look at some of these commands. We’ll start by working with images.
Working with Docker Images
Docker image helps to create a docker container. By default, Docker pulls these images from the Docker registry [Docker Hub] managed by Docker[the company behind the Docker project]. Anyone can host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need already have images hosted there.
To check whether you can access and download images from Docker Hub, type the following command:
docker run hello-world
The output will indicate that Docker is working correctly:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. ...
The Output shows that Docker was initially unable to find the hello-world image locally, so it pulling (Downloading) the image from the default repository (Docker Hub). Once the image downloaded, Docker created a container from the image and the application within the container executed, displaying the message.
You can search for images available on Docker Hub by using the docker command with the search subcommand. For example, to search for the Ubuntu image, type:
docker search ubuntu
The script will crawl Docker Hub and return a list of all images whose name matches the search string. In this case, the output will be similar to this:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 10496 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 391 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 242 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 209 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 105 [OK] neurodebian NeuroDebian provides neuroscience research s… 64 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK] ...
Here in the OFFICIAL column, OK indicates the image built and supported by the company behind the project. Once you’ve identified the image that you would like to use, you can download it to your computer using the pull subcommand.
For download the official ubuntu image to your computer, execute the following command:
docker pull ubuntu
You’ll see the following output:
Using default tag: latest latest: Pulling from library/ubuntu 5c939e3a4d10: Pull complete c63719cdbe7a: Pull complete 19a861ea6baf: Pull complete 651c9d2d6c4f: Pull complete Digest: sha256:8d31dad0c58f552e890d68bbfb735588b6b820a46e459672d96e585871acc110 Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest
After an image has been downloaded, you can create a container using the downloaded image with the run subcommand. If an image has not been downloaded when docker is executed with the run subcommand, the Docker client will first download the image, then run a container using it.
To see the images that have been downloaded to your computer, type:
The output should look similar to the following:
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB hello-world latest fce289e99eb9 13 months ago 1.84kB
Now let’s dig down run containers in more detail.
Running a Docker Container
The hello-world container we ran previously was an example of a container that runs and exits after output a message. Though containers can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.
For example, let’s run a container using the latest image of Ubuntu using the combination of the -i and -t switches which give you interactive shell access into the container:
docker run -it ubuntu
Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form:
Note the container ID in the command prompt. In this example, it is 3b08f00fe0c1. When you want to remove this container, you will need this container ID.
As you are already logged in to this container, you can run any command inside the container. For example, let’s update the package database inside the container. You don’t need to prefix any command with sudo, because you’re operating inside the container as the root user:
Then install any application in it. Let’s install GIT:
apt install git
This installs GIT in the container from the official Ubuntu repository. When the installation finishes, verify that GIT is installed:
You’ll see the version number displayed in your terminal:
git version 2.17.1
Any changes you make inside the container only apply to that container.
To exit the container, type exit at the prompt.
Now let’s look at managing the containers on our system next.
Managing Docker Containers
After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the active ones, use:
You will see output similar to the following:
CONTAINER ID IMAGE COMMAND CREATED
In this article, we started two containers; one from the hello-world image and another from the ubuntu image. Both containers are no longer running, but they still exist in your system.
To view all containers active and inactive, run docker ps with the -a switch:
docker ps -a
You’ll see output similar to this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b08f00fe0c1 ubuntu "/bin/bash" 23 minutes ago Exited (0) About a minute ago pedantic_blackwell 2cc6d98c337e hello-world "/hello" 27 minutes ago Exited (0) 10 minutes ago jovial_hopper
To view the latest container you created, pass it the
docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b08f00fe0c1 ubuntu "/bin/bash" 25 minutes ago Exited (0) 3 minutes ago pedantic_blackwell
To start a stopped container, use docker start, followed by the container ID or the container’s name. Let’s start the Ubuntu-based container with the ID of 3b08f00fe0c1:
docker start 3b08f00fe0c1
The container will start, and you can use docker ps to see its status:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b08f00fe0c1 ubuntu "/bin/bash" 27 minutes ago Up 9 seconds pedantic_blackwell
To stop a running container, use docker stop, followed by the container ID or name. This time, we’ll use the name that Docker assigned the container, which is pedantic_blackwell:
docker stop pedantic_blackwell
Once you’ve decided you no longer need a container anymore, remove it with the docker rm command, again using either the container ID or the name. Use the docker ps -a command to find the container ID or name for the container associated with the hello-world image and remove it.
docker rm jovial_hopper
You can start a new container and give it a name using the
switch. You can also use the –rm switch to create a container that removes itself when it’s stopped. See the docker run help command for more information on these options and others.
Containers can be turned into images that you can use to build new containers. Let’s look at how that works.
Committing Changes in a Container to a Docker Image
When you start a Docker container from an image, you can create, modify, and delete files just like you can with a virtual machine. The changes that you make will only apply to that container. You can start and stop it, but once you destroy it with the docker rm command, the changes will be lost.
Here I am showing you how to save the state of a container as a new Docker image.
After installing GIT inside the Ubuntu container, now we have a container running off an image, but the container is different from the image we used to create it. But we might want to reuse this GIT container as the basis for new images later.
Then commit the changes to a new Docker image instance using the following command.
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
The -m switch is for the commit message that helps you and others know what changes we made, while -a is used to specify the author. The container_id is the one we noted earlier in the article when we started the interactive Docker session. Unless we created additional repositories on Docker Hub, the repository is usually our Docker Hub username.
For example, for the user voboghure, with the container ID of 3b08f00fe0c1, the command would be:
docker commit -m "added GIT" -a "voboghure" 3b08f00fe0c1 voboghure/ubuntu-git
When we commit an image, the new image is saved locally on our computer. Later in this article, we will learn how to push an image to a Docker registry like Docker Hub so others can access it.
Listing the Docker images again will show the new image, as well as the old one that it was derived from:
You’ll see output like this:
REPOSITORY TAG IMAGE ID CREATED SIZE voboghure/ubuntu-git latest f8eb4677a7f3 6 seconds ago 186MB ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB hello-world latest fce289e99eb9 13 months ago 1.84kB
In this example, ubuntu-git is the new image, which was derived from the existing ubuntu image from Docker Hub. The size difference reflects the changes that were made. And in this example, the change was that GIT installed. So next time you need to run a container using Ubuntu with GIT pre-installed, you can just use the new image.
You can also build Images from a Dockerfile, which lets you automate the installation of software in a new image. We will check that out in our upcoming article.
Now let’s check how we can share the new image with others so they can create containers from it.
Pushing Docker Image to a Docker Repository
Now the next step after creating a new image from an existing image is to share it with a few of your friends, the whole world on Docker Hub, or other Docker registry that you have access to. To do so you need to push an image to Docker Hub or any other Docker registry. You must have an account there as well.
Here I am gonna show you how to push a Docker image to Docker Hub. To push your image, first log into Docker Hub.
docker login -u docker-registry-username
You’ll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed. It will show you the following output.
WARNING! Your password will be stored unencrypted in /home/tapan/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
Note: If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username. For the example given in the last step, you would type:
docker tag voboghure/ubuntu-git docker-registry-username/ubuntu-git
Then you may push your own image using the following command:
docker push docker-registry-username/docker-image-name
To push the ubuntu-git image to the voboghure repository, the command would be:
docker push voboghure/ubuntu-git
The process may take some time to complete as it uploads the images, but when completed, the output will look like this:
The push refers to repository [docker.io/voboghure/ubuntu-git] 53d4efe84de3: Pushed f55aa0bd26b8: Mounted from library/ubuntu 1d0dfb259f6a: Mounted from library/ubuntu 21ec61b65b20: Mounted from library/ubuntu 43c67172d1d1: Mounted from library/ubuntu latest: digest: sha256:52fe367ce8a10113601b2897e52bd8e8e3c501918943b4f85e266251813633d0 size: 1364
After pushing an image to a registry, it should be listed on your account’s dashboard, like the bellow docker hub dashboard.
You can now use “docker pull voboghure/ubuntu-git” to pull the image to a new machine and use it to run a new container.
Well, now you know how to use docker and you have your own docker image in docker hub. I hope to bring more docker article in my coming days.
Thanks for your time.