Introduction
Docker is a popular containerization tool used to provide
software applications with a filesystem that contains everything they need to
run. Using Docker containers ensures that the software will behave the same
way, regardless of where it is deployed, because its run-time environment is
ruthlessly consistent.
We can think of a Docker image as
an inert template used to create Docker containers. Images come to life with
the
docker
run
command, which creates a container by
adding a read-write layer on top of the image.
Why Docker?
Docker allows
applications to be isolated into containers with instructions for exactly what
they need to survive that can be easily ported from machine to machine.
Why Docker is famous?
The ever-present
challenge of replicating your production set up in development suddenly becomes
close to reality because of Docker.
This is why Docker
is a huge help in enabling continous integration, delivery, and deployment
pipelines. Here’s what that looks like in action:
- Your development team is able to create
complex requirements for a microservice within an easy-to-write
Dockerfile.
- Push the code up to your git repo.
- Let the CI server pull it down and
build the EXACT environment that will be used in production to run the
test suite without needing to configure the CI server at all.
- Tear down the entire thing when it’s
finished.
- Deploy it out to a staging environment
for testers or just notify the testers so that they can run a single
command to configure and start the environment locally.
- Confidently roll exactly what you had
in development, testing, and staging into production without any concerns
about machine configuration.
Pre-requisites
Virtual Machine (Ubuntu or Centos)
Create Non-sudo user
Login to server using root privilege
Once you are logged in as
root
, we're prepared to add the new user account that we will use
to log in from now on.
adduser demo
passwd demo
As root, run this command to add your new user to the wheel group
(substitute the highlighted word with your new user):
gpasswd –a demo wheel
Installing Docker
Login as non-sudo
user
wget -qO- https://get.docker.com/
| sh
$ sudo usermod –aG docker
$(whoami)
Log out and log in
from your server to activate your new groups.
Set Docker to start
automatically at boot time:
$ sudo systemctl enable docker.service
Finally, start the
Docker service:
$ sudo systemctl start docker.service
Installing Docker Compose
Now that you have Docker installed, let's go
ahead and install Docker Compose. First, install python-pip as
prerequisite:
$ sudo yum install –y epel-release
$ sudo yum install –y python-pip
Then you can install Docker Compose:
$ sudo pip install docker-compose
You will also need to upgrade your Python
packages on CentOS 7 to get docker-compose to run successfully:
$ sudo yum upgrade python*
Docker Commands
Docker Basics
Docker containers are run from Docker images. By
default, it pulls images from Docker Hub, a Docker registry.
1.
Docker Hello-World
Let’s run some Docker commands.
$ docker run hello-world
The output
shows like below
When you
execute the Docker run command, it first checks if that image is available
locally.If it doesn’t find the image, the docker client will pull the image
from Docker hub. https://hub.docker.com/_/hello-world/
It created
new container and then print the message and exited the docker container.
To see
running Docker container execute following command
$ docker ps
Since the
container is already exited, it will not show running.
If we add
the –a flag,
it will show all containers stopped or running.
$ docker ps -a
If you run
the same command again,
$ docker run hello-world
It will not
pull the image from Docker hub because it has found it locally but it created
an entirely new container.
2.
Docker example
Execute
following command to create container using the base image of Ubuntu.
–i flag is for interactive mode and –t flag will give you terminal.
$ docker run –it ubuntu
The
command-line prompt changes to indicate that we are inside the container.
Create a
file example1.txt as below
$ echo “Hello World” > /tmp/example.txt
$ cat /tmp/example1.txt
$ exit
Now create
another container
$ docker run –it ubuntu
Execute
following command to see if the example1.txt file exists.
$ cat /tmp/example1.txt
$ exit
Again
logging to first container.
$ docker ps –a
$ docker start –ai <>
$ cat /tmp/example1.txt
$ exit
3.
Delete Containers and Images
To delete
the containers
$ docker rm
To delete
the container image from local.
$ docker rmi
4.
Pushing Docker image to hub.
First
login to docker hub
$ docker login
Now Run docker
tag image with your username, repository, and tag names so that the image
will upload to your desired destination. The syntax of the command is:
$ docker tag image username/repository:tag
$ docker push username/repository:tag
Publish the
image
$ docker push username/repository:tag
Docker Compose Commands
The public Docker
registry, Docker Hub, includes a simple Hello World image. Now that we have Docker Compose
installed, let's test it with this really simple example.
First,
create a directory for our YAML file:
$ mkdir hello-world
Then change
into the directory:
$ cd hello-world
Now create
the YAML file using your favorite text editor:
$ vi docker-compose.yml
Put the following
contents into the file, save the file, and exit the text editor:
docker-compose.yml
my-test:
image: hello-world
The first line will be
used as part of the container name. The second line specifies which image to
use to create the container. The image will be downloaded from the official
Docker Hub repository.
While still in the ~/hello-world directory, execute the following command to create the container:
$ docker-compose up -d
The output should start
with the following:
Output of docker-compose up
Creating helloworld_my-test_1...
Attaching to helloworld_my-test_1
my-test_1 |
my-test_1 | Hello from Docker.
my-test_1 | This message shows that your installation appears to
be working correctly.
my-test_1 |
Let's go over the commands the
docker-compose
tool supports.
The
docker-compose
command works on
a per-directory basis. You can have multiple groups of Docker containers
running on one machine — just make one directory for each container and one docker-compose.yml
file for each
container inside its directory.
So far we've been
running docker-compose up on our own and using CTRL-C to
shut it down. This allows debug messages to be displayed in the terminal
window. This isn't ideal though, when running in production you'll want to have docker-compose act
more like a service. One simple way to do this is to just add the -d option
when you up your session:
$ docker-compose up -d
docker-compose
will now fork to the background.
To show your group of
Docker containers (both stopped and currently running), use the following
command:
$ docker-compose ps
For example, the following shows that the
helloworld_my-test_1
container is stopped:Output of `docker-compose ps`
Name Command State Ports
-----------------------------------------------
helloworld_my-test_1 /hello
Exit0
A running container
will show the Up state:
Output of `docker-compose ps`
Name Command State Ports
---------------------------------------------------------------
nginx_nginx_1 nginx -g daemon off;
Up443/tcp, 80/tcp
To stop all running
Docker containers for an application group, issue the following command in the
same directory as the
docker-compose.yml
file used to start the Docker group:
$ docker-compose stop
Note:
docker-compose kill
is also available if you need to shut
things down more forcefully.
In some cases, Docker
containers will store their old information in an internal volume. If you want
to start from scratch you can use the
rm
command to fully delete all the containers that make
up your container group:
$ docker-compose rm
If you try any of these commands from a directory
other than the directory that contains a Docker container and
.yml
file, it will complain
and not show you your containers:Output from wrong directory
Can't find a suitable
configurationfile in this directory or any parent. Are you in the right directory?
Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml
Great Article.I am a beginner & learning Various Technology.And side by side I write blog to save my information.you can visit my blog here: Introduction to Docker Compose | Docker Compose Commands
ReplyDelete