mkdir my-nginx
cd my-nginx6 Week 3 (16-Oct-2025)
6.1 Docker Introductory Exercises
6.1.1 9. Build a Docker Image from a Dockerfile
Exercise: Create a simple
Dockerfilethat uses thenginxbase image and copies anindex.htmlfile into the container.Solution:
Create a directory for your project:
- Create a file named
Dockerfile:
FROM nginx COPY index.html /usr/share/nginx/html/index.html # Expose the port EXPOSE 80- Create a simple
index.htmlwith some content in the same directory. - Build the Docker image:
- Create a file named
docker build -t my-nginx .- Expected Output: Successful build message with a new image tagged as
my-nginx.
6.1.2 10. Run a Container with a Volume
- Exercise: Run the
nginxcontainer and mount a host directory to the container. - Solution:
docker run -d -p 8080:80 -v /path/to/local/directory:/usr/share/nginx/html nginxConfirmation: Now put some files in directory
/path/to/local/directoryand check that they appear in the container in folder/usr/share/nginx/html.Note: Replace
/path/to/local/directorywith the path to your directory containing HTML files.Expected Output: A unique container ID indicating that the container is running.
Verification: Open
http://localhost:8080in a web browser to see the content of the mounted directory.
6.1.3 11. Connect to a running Container
To connect to a running container called nginx, you can use the docker exec command. This allows you to run commands inside the container and access its shell. Here’s how you can do it:
docker exec -it nginx /bin/bash6.1.4 Explanation:
docker exec: This command runs a command in a running container.-it: This flag allows you to interact with the container (interactive mode and pseudo-TTY).nginx: The name or ID of the running container you want to connect to./bin/bash: The shell you want to use. If your container doesn’t havebash, you can try/bin/shinstead.
After running this command, you will be inside the nginx container’s shell, where you can navigate and execute commands as needed.
Pushing an image to Docker Hub involves a few straightforward steps. Here’s a complete example of how to do it:
6.1.5 12. Log In to Docker Hub
- Registration: Open Docker Hub Registration (https://hub.docker.com) in a web browser to register in Docker Hub.
Before you can push an image to Docker Hub, you need to log in:
docker loginYou will be prompted to enter your Docker Hub username and password.
According with the username you created on Docker Hub, build your image with that username. (for example: if your username is
jcppc).Build the Docker image:
docker build -t jcppc/my-nginx .
docker build -t yourusername/my-nginx .6.1.6 13. Push the Image to Docker Hub
Now you can push your image to Docker Hub:
docker push yourusername/my-nginx:latest6.1.7 14. Verify the Image on Docker Hub
After a successful push, you can verify that your image has been uploaded by visiting your Docker Hub profile here and looking for yourusername/my-nginx in your repositories.
6.1.8 Conclusion
You have successfully created a Docker image for a simple Website application, built it, and pushed it to Docker Hub. You can now share this image with others or deploy it on various platforms that support Docker.
These exercises introduce basic Docker concepts, from running containers to creating images and using volumes. Completing them will give you a solid foundation in Docker usage.