# Workshop: Nginx
WARNING
- Please prepare one folder for this Workshop
# Step 1: Create files
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hi Hello SAD Class</h1>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Dockerfile
FROM nginx:stable-alpine
COPY ./index.html /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
1
2
3
4
5
2
3
4
5
- COPY: copy files from host to Image
# Step 2: From Dockerfile to create Docker Image
docker build . -t my-web:v1
1
# Step 3: Run Docker Container from your Image
- check your Image
docker image ls
1
output
REPOSITORY TAG IMAGE ID CREATED SIZE
my-web v1 9a6a38822c69 7 minutes ago 23.2MB
1
2
2
- run
docker run -d -p 80:80 my-web:v1
1
-p publish: map {host port}:{container port}
Finally, open browser and go to http://localhost (opens new window)
# Step 4: Stop and remove container
docker container stop {container_id}
docker container rm {container_id}
1
2
2
or
docker stop {container_id}
docker rm {container_id}
1
2
2
or
docker container rm {container_id} -f
1
or
docker rm {container_id} -f
1
- find the container id
docker ps
1
or
docker container ls
1
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
084e8bd8e49d my-web:v1 "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp clever_lalande
1
2
2
docker rm 084 -f
1
- PS. you can use the short id.
# Step 5-0: Discussion
- Next, we will proof if the container changes the state (ex. add more files, file changing or something which makes your container isn't same as original Image) and you might remove it (docker rm). Then, you create the container again (docker run). What you have changed before would be gone too.
- In another word, it looks like you have VM. You have code in it. You have data. Then, you delete it. If you create a new VM, you would get new VM. However, please keep it in your mind, container isn't VM. Container is a process.
# Step 5: Create a new Container
docker run -d -p 80:80 my-web:v1
1
# Step 6: Go inside Container
# Discussion
Dockerize an SSH service (opens new window)
Running sshd inside a container is discouraged, however, it might be still useful for certain use cases such as port forwarding.
See https://github.com/linuxserver/docker-openssh-server for an example of Dockerized SSH service.
- Basically, we don't need to use ssh to go inside Docker container. What we can do is Run a shell command to our container.
docker exec -it {container_id/NAME} {command}
1
- docker exec (opens new window)
- -it for interactive shell
- There are many shell commands. For example, bash, sh, zsh, and more depend on Base Image.
- nginx container does not have bash but it has sh
- find ID
docker ps
1
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19c5def99721 my-web:v1 "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 0.0.0.0:80->80/tcp keen_wiles
1
2
2
- run shell command
docker exec -it 19c sh
1
output
/ # |
1
- Now, we are in nginx container.
- make some states change (we will add some files)
echo HelloSADClass > hello.txt
ls
1
2
2
output
bin docker-entrypoint.sh home mnt root srv usr
dev etc lib opt run sys var
docker-entrypoint.d hello.txt media proc sbin tmp
1
2
3
2
3
- saw hello.txt
- exit from this container
exit
1
# Step 7: remove old container and create a new one.
- rm and run
docker rm 19c -f
docker run -d -p 80:80 my-web:v1
docker ps
1
2
3
2
3
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c20808a474ac my-web:v1 "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp jolly_colden
1
2
2
- new ID new Container
- make sure that hello.txt is not here
docker exec -it c20 sh
1
ls
1
output
bin docker-entrypoint.sh lib opt run sys var
dev etc media proc sbin tmp
docker-entrypoint.d home mnt root srv usr
1
2
3
2
3
- exit from container
exit
1
- rm container and finish this lab
docker rm c20 -f
1