Docker-interact with docker engine and client
Docker-interact with docker engine and client
Demonstration of simple interaction between Docker client and the Docker daemon. We
will use Docker CLI to run a sample container and it’s going to be a container called “html-
app”, which I made for using in demonstration. Web app is containerized portfolio website
for various testing purposes.
So we are going ahead and download a Docker image from my PUBLIC repository, from
Docker host (Docker Hub).
Steps:
This command will list any containers running on your Docker host (docker host is
your computer). On the picture we can see that we don’t have ANY RUNNING
CONTAINERS.
Checking for docker images that are downloaded on my Docker host. In your case maybe
it will not show anything, but in this moment you will see that I have few downloaded
images after running command:
$ docker images
This is not clean Docker host, because I have a few images previous downloaded on my
host.
Commands to remmember:
User can download own docker image from docker hub, but it can also use already
provided images from docker hub or create own docker image and later use it for different
purposes. To create own image, user need to learn some other things which will be
explained in other demonstration.
I will show you how to use provided docker image but also docker image which is created
by myself so you can see the difference.
One of the options which user can do it is to download provided application hello-world
from Docker Hub and then run it on Docker Host (your computer).
How user will download the provided docker image from docker hub?
Docker Hub
3. Using that Docker image to spin up a container
If you don’t have already downloaded this docker image, the command will immediately
downloaded it on your docker host.
One of the first messages which you can get after the command is “Unable to find image
‘hello-world:latest’ locally.
This message means that this image is not stored on our Docker Host (computer). The
word “:latest” is TAG and this only means the latest version of image.
After this message you will see: “latest: Pulling from library/hello-world” . Because it
can’t find image locally, it’s going to pull this image down on our local computer (machine,
host). After pulling (download) the image, we will see some output on the screen.
This image (hello-world) is now running locally on our Docker Host and the it runs a
container using this image and we get this output.
1. First message tell us that Docker client contacted the Docker Daemon
2. Second message tell us that the Docker Daemon pulled Hello World from Docker Hub
3. Third message tell us that the Docker Daemon started new container from that
image which is running the executable that produces the output that we’re currently
reading
4. Forth message tell us that after these then the Docker Daemon streamed that output to
the Docker client , which sent it to your terminal.
In one sentence, this mean that we pulled and image from Docker Hub to our local Docker
host and we’ve used that to run a container.
The process of pulling down this image is showed below on the next page.
If we run docker on it’s own and we don’t have a copy, on the local host, of the relevant
image(hello-world), then it pulls that image automatically on our host.
We will again use command: $ docker ps, and it will show us next output:
Why?
We have this output, because we don’t have any running containers. This is happening
because the container that we just run all it did it was output that text and then stopped.
If we run command, $ docker ps -a , allows us to see in detail when the last time
container is ran with his ID (unique), the name of image, how long that container was
working. In this case, it was 3 minutes because it’s just output the text and exit.
Run the command $ docker images , which will show us any images locally stored on
the Docker host.
If you want to download own image or the docker image from another person, you will use
command $ docker pull ID/name-of-image
After running this command, we have a copy of this Docker image on our local machine. If
you run a command to see downloaded images, $ docker images, we can see that this
docker image is downloaded (in my case before 10 month ago) and it has a unique image
ID. On your machine this unique ID it will be different.
We can use INSPECT to check information. In terminal you need to type command:
IMAGE-ID → ID which you have after you download docker image (copied) and then you
to press enter. You will get information when is created, associated labels, networks,
volumes. Metadata is in JSON format. This allows for debugging , troubleshooting and
understanding the internal workings of Docker deployments. On the Docker
documentation, you can see useful commands to get deeply in command to find, debug,
resolve the problem: https://docs.docker.com/reference/cli/docker/image/inspect/
Run the command $ docker images so we can see image ID (but also the name of image
can be used for most commands).
Using this command we want to tell Docker engine to take port 80 on this Container,
because this is the application within this Container use, so we want to map port 80 to port
80 81 on our Docker host, because that is way how we are going to access it. Port 8081 is
on our local machine, so we can access this application.
NOW, port 80 is port within the Container that this application is running on.
After mapping we need to identify which image we will use, and add that part to the
command to have full command:
After running the command, type in your browser: http://localhost:8081/ . If you have
done everything on the right way, then it should appear your own docker image, running in
the web browser locally. In my case it looks like this in the browser:
This application have output of portfolio website running in a Container. To stop container
to work, we need to come back to terminal and press CTRL+C and stop. After we stop, if
we return to browser, you can see that there is no more website. Why? Because we stop
this container to running using command CTRL+C. It’s double function, back to terminal
but also stop the container to running.
Checking running containers: $ docker ps, after running this command you can see that
there is no any container running. If you type $ docker ps -a , you can see that container
is only in Exited status , but not in running state.
When we want to use container, example website-jpl, we will run the command $ docker
run -p 8081:80 -d name-of-image . This command will detach our terminal from
Container. On this way, our docker container will be still in running mode and user can
see that still in browser, in local host, on the same port the website.
After running you will see unique code for this container, but in browser you can see again
the page when you refresh it.
In terminal we can check that the docker container is still running using command $
docker ps, in part Status it will be “Up 16 minutes” status.
In detached mode we don’t need to keep terminal open, we can continue using terminal in
this mode and container will be in status running.
Command $docker exec -it 77f974efef15 sh will run a shell inside the Docker Container
and connect us to that shell. Command df -k will show us the file system inside of
Docker Container.
To exit from shell, inside of the Docker Container you need to type $ exit.
To get ContainerID, the best thing is to run command docker ps and to check containerID.
DIAGNOSTIC CONTAINER
$ docker logs containerID - this will show us any logs for this containerize
$ docker logs containerID -t → this command will show us timestamp and this is simple
diagnostic with a Container
How to clean , stop everything if you want to have clear docker host?
1. Run $sdocker ps
2. Stop the container $docker stop containerID
3. Removing container using command $docker rm containerID
4. Checking is there running container: $docker ps
5. Check other containers are running to remove them $docker ps -a
6. Check for docker images: $ docker images
7. Remove images which we have locally stored: $ docker rmi imageID
COMMANDS IN THIS PART
$docker inspect image-id →
$docker run -p 8081:80 name-of-image →
$docker ps -a → list all containers (including stopped)
$docker run -p 8081:80 -d name-of-image → DETACH current terminal from container
$docker port ContainerID → mapping port Configuration of running
Container
$docker exec -it ContainerID ps -aux → check process running on container
$docker exec -it ContainerID sh → running shell inside of the docker container (after
enter, we are inside of Docker Container)
$docker start ContainerID
$docker restart ContainerID
$docker stop ContainerID
Copyright: Suncica P.