Introduction to docker. Developing a simple docker image for your own server.
== Task ==
$ docker pull nginx
$ docker images
$ docker run -d [image_id]
$ docker ps
$ docker inspect [image_id]
From the command output define and write in the report the container size, list of mapped ports and container ip
Or you may use $ docker container ls -s
to check container parameters
$ docker stop [image_id]
$ docker ps
$ docker run --name my-nginx2 -p 80:80 -p 443:80 -d [image_name]
after it you can check which ports it's using
$ docker container ls -s
after all of that you should see the nginx start page on localhost:80 and localhost:443 in browser
$ docker restart [container_id]
$ docker container ls -s
Docker image and container are ready. Now we can look into nginx configuration and display page status.
== Task ==
$ docker exec -it [container_id] /bin/bash
# cd etc/nginx
# touch nginx.conf
First, you should check, where is nginx status page locates. You can find it in conf.d/default.conf
in same directory as nginx.conf
.
index.html
contains in /usr/share/ngninx/html
by default
Then copy
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
include /etc/nginx/default.d/*.conf;
#gzip on;
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
}
location /status {
root /usr/share/nginx/html/;
stub_status;
index index.html;
allow all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
to your ngnix.conf
file
$ docker cp nginx.conf CONTAINER_ID:path
Before you apply changes you can use # nginx -t
in container to check them.
Then you can use
$ docker exec -it [container_id] nginx -s reload
Afeter reloading you shoul see on localhost:80/status this content:
$ docker export CONTAINER_ID > filename.tar
$ docker stop CONTAINER_ID
$ docker image ls
to show list of installed images
$ docker rmi -f IMAGE_ID
$ docker ps -a
list of all containers
$ docker rm CONTAINER_ID
$ docker import filename.tar
- Add the following screenshots to the report:
- the call and output of all commands used in this part of the task;
- the contents of the created nginx.conf file;
- the nginx server status page at localhost:80/status.
It's time to take a little break from the docker to prepare for the last stage. It's time to write your own server.
== Task ==
Now everything is ready. You can start writing the docker image for the created server.
== Task ==
When writing a docker image avoid multiple calls of RUN instructions
nginx can be installed inside the docker itself, or you can use a ready-made image with nginx as base.
Run the built docker image by mapping port 81 to 80 on the local machine and mapping the ./nginx folder inside the container to the address where the nginx configuration files are located (see [Part 2](#part-2-operations-with
-container))
*If everything is done correctly, after saving the file and restarting the container, the configuration file inside the docker image should update itself without any extra steps
Once you've written the image, it's never a bad idea to check it for security.
== Task ==
There, you've finished your warm-up. Wait a minute though... Why not try experimenting with deploying a project consisting of several docker images at once?
== Task ==
1) Start the docker container from Part 5 (it must work on local network, i.e., you don't need to use EXPOSE instruction and map ports to local machine)
2) Start the docker container with nginx which will proxy all requests from port 8080 to port 81 of the first container
💡 Tap here to leave your feedback on the project. Pedago Team really tries to make your educational experience better.