Docker
docker ps
List of running containers
docker ps -a
List of running containers and also exited containers
docker rm CONTAINER_ID
Remove container
docker run hello-world
We are telling docker that we want to container with image called hello-world
. Docker tries to find it locally. As it wasnt successfull, it was downloaded from Docker Hub (public images repository). It shows as message Hello from Docker
, that is message from the container. Script finished and container turned off.
michalmachovic@michalmachovic:/var/www/html$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
docker run -it --name mujDebian debian bash
We will create our own container called mujDebian
and start process bash
.
var/www/html$ sudo docker run -it --name mujDebian debian bash
Unable to find image 'debian:latest' locally
latest: Pulling from library/debian
22dbe790f715: Pull complete
Digest: sha256:72e996751fe42b2a0c1e6355730dc2751ccda50564fec929f76804a6365ef5ef
Status: Downloaded newer image for debian:latest
We are now inside container in Debian
. We will install simple static server, then we will create config file and simple html page. Finally we exit
from container.
root@d1cbba435b85:/# apt-get update && apt-get install lighttpd -y
root@d1cbba435b85:/# echo 'server.document-root="./"' > lighttpd.conf
root@d1cbba435b85:/# echo 'server.port=80' >> lighttpd.conf
root@d1cbba435b85:/# echo "
<H1>Tohle je muj web</H1>
" > index.html
root@d1cbba435b85:/# exit
docker diff mujDebian
This will show differences which we made in container - installation of webserver and simple html and config file
docker diff mujDebian
...
C /var/log/dpkg.log
A /var/log/lighttpd
A /var/www
A /var/www/html
A /var/www/html/index.lighttpd.html
Lets create new image from this our container.
docker commit mujDebian michalmachovic:mujWebImage
docker images
will show us all docker images
docker run -d -p 9080:80 --name mujWeb michalmachovic:mujWebImage lighttpd -D -f lighttpd.conf
Now we will run new container with this image.
-d means that we will run it in background
-p means that we want to connect to service which container offers from outside. In our case this will be simple webserver, which we created inside container running on port 80
, to the outside Docker will serve this on port 9080
.
curl 127.0.0.1:9080/index.html
should show our page
Our container has disadvantage, it is static. What happen if we will want to start from another version of Debian ? Or another version of webserver ? We need to do container again manually. We can automate this.
On your local computer, create new directory and create the same index.html
and lighttpd.conf
files. Also in this folder create Dockerfile
with following content:
FROM debian
RUN apt-get update && apt-get install lighttpd -y
EXPOSE 80
ADD lighttpd.conf .
ADD index.html .
CMD lighttpd -D -f lighttpd.conf
FROM - from which container we want to start
RUN - will be executed after container start, it will create whole new layer, its good to put all installations into one RUN
EXPOSE - saying there is something running inside container on port 80
ADD - add files from local directory to container
CMD - this will happen after start of new finished container, in our case it will run lighttpd webserver (so we dont need to type it manually)
docker build -t michalmachovic:dalsiWeb .
build our new container, check it exists with docker images
.
docker run -P --name dalsiWebKontejner2 michalmachovic:dalsiWeb
lets run our new container with -P
parameter, we specified inside port with EXPOSE
, so this will assign some free port on host system.
You should something like 2019-03-13 10:42:01: (log.c.217) server started
.
Now if you do docker ps
in new terminal window, you should see something like:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6087dcae5e79 michalmachovic:dalsiWeb "/bin/sh -c 'lighttp…" 39 seconds ago Up 39 seconds 0.0.0.0:32768->80/tcp dalsiWebKontejner2
You can see 0.0.0.0:32768->80
, that means it will run on port 32768
, you can test this with curl 127.0.0.1:32768/index.html
.
http://www.cloudsvet.cz/?p=198
https://www.zdrojak.cz/clanky/proc-pouzivat-docker