The goals of this lab are to:
- Install Docker.
- Write a Dockerfile and build a Docker image.
- Run a Docker container with multiple options.
- Share your Docker container with a classmate.
- Build and run a multiple container app with docker compose.
In the hello-world-web-app
directory, you will find:
- A
server.js
which is the code for a simple "Hello World" Node.js web app - A
package.json
that describes the Node.js web app and its dependencies - A
Dockerfile
which describes the previous Node.js web app as a Docker container
Before you can start the lab, you have to:
- Install Docker following the instructions depending on your OS.
- Make sure your docker installation in working properly by running the following command in a terminal (Cmd or Powershell for Windows):
docker run hello-world
- Clone the lab repository to your computer:
git clone https://github.com/leopaul36/dsti-fall-devops-docker.git
- Open the
hello-world-web-app
directory in the repository and check out theserver.js
,package.json
andDockerfile
files - Check out the explanations for each line in the Dockerfile from the documentation
- Build the docker container
- Open a terminal (Cmd or Powershell for Windows)
- Navigate to the
hello-world-web-app
directory in the cloned repository - Run the following command:
Don't forge the
docker build -t hello-world-webapp-container .
.
at the end of the command. It is here to tell Docker it should look for a Dockerfile in the current directory-t
tags the built container with the name you want, herehello-world-webapp-container
- Check if your Docker container appears in the local Docker images:
docker images
- Run the container with the following command:
docker run -p 12345:8080 -d hello-world-webapp-container
-p
maps a port on your local machine to a port inside the container-d
makes the container run in background
- Check if the container is running (and save the container ID) with the following command:
docker ps
- Open your web browser and go to
http://localhost:12345
- Print the logs of the container with:
docker logs <container id>
- Stop the container with:
docker stop <container id>
- Modify the message printed in the
server.js
(you can add your name for example) - Rebuild the Docker container (with a different name) with this modified code and see if you can run it and navigate to the web app in your browser
- Register on Docker Hub
- Tag your container with the following command:
docker tag hello-world-webapp-container <docker-account-name>/<custom-image-name>
- Push the docker image to Docker Hub:
docker push <docker-account-name>/<custom-image-name>
- See if you can find the image in your repositories in the Docker Hub
- Ask a classmate to retrieve your Docker container and run it:
docker pull <docker-account-name>/<custom-image-name> docker run -p 12345:8080 -d <docker-account-name>/<custom-image-name>
-
Docker compose should be included in your Docker installation (on Windows and Mac at least), if not install it using the official instructions
-
Navigate to the
hello-world-compose-web-app
directory in the cloned repository -
Build the Docker image with the name of your choice
-
Fill the missing part of the
docker-compose.yaml
file to make it use the container you just built -
Start the containers with
docker-compose up
-
Visit
localhost:5000
in your web browser and hit refresh a couple times -
Stop the containers by running
CTRL+C
in the previous terminal -
Delete the containers with:
docker-compose rm
-
Start the containers again
- What happened to the counter? Why?
- Delete the containers again
-
Make the necessary changes in the Docker compose file so that when you delete and create the containers again the counter keeps its value
Hint: Use volumes, the redis container stores its data in the /data directory
- Run Wordpress with MySQL as a Docker compose app