Using Docker on CentOS 7

Basic commands and techniques for working with Docker on CentOS 7.

Docker is software for automating the deployment and management of applications using container-level virtualization. It lets you package an application with all its dependencies into an isolated container that can be moved to any Linux system with cgroups support.

Docker

For installation instructions, see Installing Docker on CentOS 7.

Basic commands

The docker command syntax:

docker [option] [command] [arguments]

List all available subcommands:

docker

Get help for a specific subcommand:

docker docker-subcommand --help

Display general Docker information:

docker info

Working with images

By default, Docker pulls images from Docker Hub. To verify access to the registry, run the test container:

docker run hello-world

Expected output:

Hello from Docker!
This message shows that your installation appears to be working correctly.

Search for an image:

docker search nginx

The OK value in the OFFICIAL column means the image is maintained by the project's developer.

Pull an image:

docker pull nginx

Run an image:

docker run <name>

List downloaded images:

docker images

Example output:

REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
nginx               latest    602e111c06b6   12 days ago    127MB
centos              latest    470671670cac   3 months ago   237MB
hello-world         latest    bf756fb1ae65   4 months ago   13.3kB

Running a container

To start an interactive container with shell access, use the -it flags:

docker run -it centos

The prompt will change to the container's shell:

[root@dadc89ffcb35 /]#

From here you can run any commands inside the container. For example, install MariaDB:

yum install mariadb-server

All changes apply only to the current container. Type exit to leave.

Managing containers

List active containers:

docker ps

List all containers, including stopped ones:

docker ps -a

Show the most recently created container:

docker ps -l

Start a stopped container:

docker start dadc89ffcb35

Stop a running container:

docker stop nifty_jang

Remove a container:

docker rm nifty_jang

Use docker ps -a to find the container ID or name.

Saving changes to a new image

After modifying a container, you can save the changes as a new image using docker commit:

docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
  • -m — commit message describing the changes.
  • -a — author name.
  • container_id — the container's ID.
  • repository — your Docker Hub username.

For example:

docker commit -m "added mariadb-server" -a "test" dadc89ffcb35 test/centos-mariadb

The new image will appear in your local image list:

docker images

Example output:

REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
test/centos-mariadb   latest    bd8ad6193efb   29 seconds ago   493MB
nginx                 latest    602e111c06b6   12 days ago      127MB
centos                latest    470671670cac   3 months ago     237MB
hello-world           latest    bf756fb1ae65   4 months ago     13.3kB

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us