Be in the folder where the Dockerfile exists
┌──(kali㉿kali)-[~/ctf/WebCTF]
└─$ ls
challenge distfiles docker-compose.yml Dockerfile solution
(sample output)Now run the following command using sudo privileges:
┌──(kali㉿kali)-[~/ctf/WebCTF]
└─$ docker build -t www-data/chall1 .docker build: This part of the command instructs Docker to build a new Docker image.t www-data/chall1: Thetflag is used to specify a name and optionally a tag to the Docker image being built. In this case, the image is being tagged with the namewww-data/chall1. A tag in Docker is a label assigned to a specific image version. It's useful for versioning and managing different variations of the same base image.- where
chall1is a sample unique name for every challenge you might want to build. .: The dot (.) at the end of the command represents the build context. The build context is the set of files located in the current directory and its subdirectories. These files are sent to the Docker daemon to be used in the build process. In other words, Docker will look for a Dockerfile in the current directory (and its subdirectories) to build the image.
Now use the following command which is explained below
┌──(kali㉿kali)-[~/ctf/WebCTF]
└─$ sudo docker run –d –p 8080:8080 www-data/chall1sudo: This command is used in Unix-like operating systems to allow a permitted user to execute a command as the superuser or another user, as specified in the security policy configured in thesudoersfile. It's often required for running Docker commands, especially if you're not the root user.docker run: This part of the command is used to run a new Docker container based on a specified image.d: This flag stands for "detached" mode. When you run a container in detached mode (d), it means the container will run in the background without blocking your terminal. You'll get the command prompt back immediately after executing the Docker run command.p 8080:8080: This flag is used to publish a container's port to the host. In this case, it maps port 8080 on the host to port 8080 on the container. This means that you can access the services inside the container, which are running on port 8080, from your host machine by connecting to port 8080.www-data/chall1: This specifies the name of the Docker image from which the container will be created.
After running the command sudo docker run -d -p 8080:8080 www-data/chall1, several things happen:
- Docker Image Check: Docker checks if the
www-data/chall1image exists locally. If the image does not exist, Docker will attempt to pull it from a Docker registry (like Docker Hub) before creating the container. - Container Creation: Docker creates a new container based on the specified image (
www-data/chall1). This container is an instance of the image, isolated from the host system and other containers. - Detached Mode: Because of the
dflag, the container runs in detached mode. This means it runs in the background, allowing you to continue using your terminal for other commands without being attached to the container's console. - Port Mapping: The
p 8080:8080flag maps port 8080 on the host machine to port 8080 in the container. This enables you to access services inside the container vialocalhost:8080on your host machine. - Background Execution: The container starts running in the background, and you receive a unique container ID as output. This ID can be used to manage the container later.
- Container Management: You can use various Docker commands to manage the running container. For example, you can stop the container using
docker stop <container_id>, remove it usingdocker rm <container_id>, or inspect its logs usingdocker logs <container_id>. - Service Access: If the application inside the container provides a service (like a web server), you can access that service by visiting
http://localhost:8080in your web browser or by making HTTP requests tolocalhost:8080using tools likecurlor web browsers. The requests will be routed to the application running inside the container on port 8080.