How To Create, Start, Stop, Manage Docker Containers In Linux?
Docker containers have created new momentum in the virtualization and container area. Docker containers are processes running on the host operating system in a restricted environment.
Docker engine is used to manage the life cycle of the containers with various commands. We will look these sub-commands in this tutorial.
Now some terminology. Docker ecosystem provides a lot of different systems and terms about the work. A container is data and process that is running in a restricted way in a host as process. Generally working state is called container.
Docker image is the data files provided by different repositories. Docker image generally provides base operating system files and user spaces files related with the image.
Docker hub is popular and most used hub for providing docker images. As the creator and maintainer it is very popular and comes by default with Docker engine. Docker hub provides image for Ubuntu, Apache, CentOS, RabbitMQ, …
There are also alternative hubs those provides paid or more specialized Docker images. Also we can create some private Docker hubs.
Pull Container Image
Docker Images are downloaded from repositories like Docker Hub. In order to start a container we need to get Image of the container. This is called pulling of the image. Pulling image will download related image files from repository to the local Docker image location. Default docker image location is
/var/lib/docker/image . We will use
pull command. In the example we will download the
Fedora image from Docker Hub.
$ sudo docker pull fedora
As we can see from screenshot while downloading an image just image name is not enough. We should also provide a tag which will specify exact image. But there is a shortcut provided by docker. If no tag is provided docker assumes tag
latest by default.
We have downloaded image for
Fedora in previous example. Now we can create a container without starting it. Actually a container can be started directly without creating it which will create automatically. Creating container will not start it. We will use
create subcommand for this. In this example we will create container by giving name to it with
--name option. The name of the new container will be
$ docker create fedora --name myfed
After creation is completed a hash values is provided. This id the ID of the container and used for specify container in the various container related operations.
Start Docker Container
In this step we will start container by specifying the base image name. Starting container will create new process in the host operating system according to the container parameters. We will use
run command with various options where we look them in detail next examples.
$ docker run fedora /bin/bash
After command operation completed the container will stop. This is the nature of the containers. They starts, runs and stops generally in limited times.
Connect Container To Terminal
Previous example we have started container but after the command complation the session end. If we need connect to the terminal of the container and want to issue commands we should provide
-t parameters like below.
$ docker run -t -i fedora /bin/bash
The container is no stopped after command operation. We can issue commands here as
root user of the container. If we do not provide a host name the host name is provided as random.
We may need to list currently running containers in a host. We will use
ps command for this. For more information about the container status read following post.
$ docker ps
Currently running containers names can be changed. But keep in mind we do not change the ID we will change the name which is like a tag. We will use
rename command with container id and new name. In the example the container id is f991c8869664 and new name will be set as
$ docker rename f991c8869664 mynewname
We can stop the Docker container with
$ docker stop f991c8869664
Delete and Remove Container
Containers are generally used for limited time. So after the use of container ends the container will be deleted and related resources will be freed. We can delete a container with
rm command which is similar to Linux bash. In order to remove a container the container should be stopped as defined previous example. We will also provide container id for remove. In this example we remove the container with id f991c8869664
$ sudo docker rm f991c8869664
Pausing container maybe needed some times to cut down CPU usage. We can pause a container with
pause sub-command by providing container id.
$ docker pause a264a020b160