Docker Quickstart Guide: Working With Images and Containers

Leave your reply

Introduction

Learn how to work with Docker containers and images. In this article we will cover basic Docker commands and information.

Note: For any Cloud Server with Plesk, applications like Docker should always be installed and managed through the Plesk interface. See our article Use Docker on a Cloud Server With Plesk for step-by-step instructions.

Requirements

  • 1&1 IONOS Linux Cloud Server
  • Docker installed and running

Terminology: Container vs Image

New Docker users often find themselves wondering about the difference beween a Docker container and a Docker image.

A Docker image is a collection of filesystem changes and parameters. It is a snapshot of a system.

A Docker container is a running instance which has been launched using a Docker image.

Safely Working With Docker Without Using Sudo

The Docker daemon runs as root, which means that users will need to use sudo to run Docker commands.

To avoid having to use sudo for every Docker command, simply add your user(s) to the docker group with the command:

usermod -aG docker [username]

For example, by adding the user jdoe to the Docker group, this user will no longer have to use sudo for every Docker command. To add the user to the group, use the command:

usermod -aG docker jdoe

Finding a Docker Image

The first step to running Docker is locating an image to use.

Finding a Docker Image on the Docker Website

There are many pre-made images available on the Docker website. You can find them from the command line with the docker search command.

For example, to search for a CentOS image, run the following command:

sudo docker search centos | more

This will return a list of all the CentOS images available on the Docker website.

Finding a Docker Image on Your System

To list all of the images available on your system, run the following command:

sudo docker images

Downloading a Docker Image

To start using Docker, you will need on your host machine an image that Docker can use to launch a container. You can do this with the command:

sudo docker pull [image name]

You can find the image name using the docker search command as detailed in the previous section.

For example, if we search for a CentOS image using the command docker search centos | more the first result is the official CentOS build, an image named centos.

Docker search for CentOS image

To download this image, run the following command:

sudo docker pull centos

Running a Docker Container

You can set up a basic container with a Bash shell using just one command:

sudo docker run --name [container name] -i -t [image name] /bin/bash

For example, to set up a basic container named `test_centos_container` using the CentOS 7 build, which we downloaded in the previous section, run the following command:

sudo docker run --name test_centos_container -i -t centos /bin/bash

Note: The -i flag attaches stdin and stdout and -t flag allocates a tty.

After running this command, you will be using a Bash shell inside a CentOS 7 Docker container.

To disconnect or detach from the container, use the escape sequence Ctrl-p + Ctrl-q.

Using Docker Container Names

If you do not specify a name for your Docker container, Docker will randomly generate one for you. Although names are not required, they can be convenient ways to identify your Docker containers.

We recommend using the --name flag whenever you run a Docker container, and choosing a useful name for your container. For example, my_container_3 is a less useful name than centos_with_firewall_configured or ubuntu_with_lamp_stack.

Disconnecting (Detaching) From a Docker Container

To disconnect or detach from the shell without exiting, use the escape sequence Ctrl-p + Ctrl-q.

Connecting (Attaching) to a Running Docker Container

If you want to work with a running Docker container, you can use the docker attach command to connect (attach) to a Bash shell on it.

sudo docker exec -it [container ID or name] bash

Note: You can list all container IDs and names by using the docker ps -a command.

For example, to attach to a Bash shell on the previously-created container, named test_centos_container, run the command:

sudo docker exec -it test_centos_container bash

To disconnect (detach) from the container, use the escape sequence Ctrl-p + Ctrl-q.

Listing Docker Containers

You can use the following command to list all running and non-running containers:

sudo docker ps -a 

Stopping a Running Docker Container

To stop a container's process, use the following command:

sudo docker stop [container ID or name]

Note: You can list all container IDs and names by using the docker ps -a command.

For example, to stop the previously-created container, named test_centos_container, run the command:

sudo docker stop test_centos_container

Starting a Stopped Docker Container

Sometimes a container will stop, either because you have stopped it, or because its process has ended. In this situation you can run the container again with the container ID or name.

sudo docker start [container ID or name]

Note: You can list all container IDs and names by using the docker ps -a command.

For example, to start the previously-stopped container, named test_centos_container, run the command:

sudo docker start test_centos_container

Saving a Docker Container as an Image

If you would like to save the changes you have made to a running container, use docker commit to save the container as an image.

sudo docker commit [container ID or name] [image name]

Note: You can list all container IDs and names by using the docker ps -a command.

For example, to save the running container test_centos_container as an image named test_centos_image, run the command:

sudo docker commit test_centos_container test_centos_image

This will result in an image named test_centos_image which you can run as a container. You can list all images on your system with the command:

sudo docker images

Deleting a Docker Container

A stopped Docker container will continue to exist on your system. Use the docker rm command to tidy up your system and delete unwanted Docker containers.

sudo docker rm [container ID or name]

Note: You can list all container IDs and names by using the docker ps -a command.

For example, to delete the container named test_centos_container which we created above, run the command:

sudo docker rm test_centos_container

Deleting a Docker Image

All the Docker images you download or create will be saved on your system. Use the docker rmi command to tidy up your system and delete unwanted Docker images.

sudo docker rmi [image name]

Note: You can list all container IDs and names by using the docker ps -a command.

For example, to delete the image named test_centos_image, run the command:

sudo docker rmi test_centos_image