Docker Lab Exercises
Day-3
Creating an Image From a running Container
1. Run an Ubuntu Container and Enter Interactive Shell:
docker run --name ub_1 -it ubuntu
- This command starts a new Docker container named "ub_1" based on the official Ubuntu image and opens an interactive shell within the container.
Example Output: You will be inside the shell of the newly created container.
2. Install Java on Ubuntu:
After entering the container, you can install Java and update packages as you mentioned earlier. For brevity, we'll assume you've already done this within the container.
3. Install Vim Inside the Container:
apt-get install vim -y
- This command installs the Vim text editor inside the container.
Example Output: The `vim` package will be installed, and you will see installation progress and messages within the container.
4. Exit the Container Without Stopping:
To exit the container without stopping it, you can simply run:
Press Ctrl + P
followed by Ctrl + Q
.
- This will exit the interactive shell session within the container and return you to the host system's terminal.
Example Output: You will exit the container and return to your host system's shell.
5. Commit Changes to Create a New Docker Image:
docker commit ub_1 my-image:vim
- This command creates a new Docker image named "my-image" with the tag "vim," based on the changes you made to the "ub_1" container (i.e., installing Vim).
Example Output: It will provide a unique image ID.
6. Run a New Container from the Custom Image:
docker run -it my-image:vim
- This command starts a new Docker container from the "my-image:vim" image, which includes Vim installed.
Example Output: You will be inside a new container with Vim installed, similar to the previous container.
7. Exit the New Container:
To exit the new container without stopping it, you can use the `exit` command within the container, just like before.
8. List Docker Images:
docker images
- This command lists all Docker images on your system, including the "my-image:vim" image you created.
Example Output: It will display a list of Docker images, including "my-image:vim."
9. Tag the Custom Image:
docker tag my-image:vim dnvsraj/repo-1:vim
- This command tags your custom image "my-image:vim" with a new repository and tag name, "dnvsraj/repo-1:vim." This is useful for pushing the image to a Docker registry.
Example Output: No output is generated for this command.
10. Push the Custom Image to Docker Hub (Assuming you have authentication):
docker push dnvsraj/repo-1:vim
- This command pushes the tagged image to the Docker Hub repository "dnvsraj/repo-1" with the "vim" tag.
Example Output: If authentication is successful and you have the necessary permissions, the image will be pushed to the specified Docker Hub repository. You will see progress messages indicating the push operation's progress.
Please note that successful execution of the `docker push` command depends on your Docker Hub authentication and permissions. If you are not authenticated or lack the necessary permissions, you will encounter an error during the push operation.
Create Docker Image From a Docker File
1. Create a Directory and Change into it:
mkdir MYDIR
cd MYDIR
- These commands create a directory named "MYDIR" and navigate into it.
2. Create a Dockerfile:
You need to create a Dockerfile named `Dockerfile` within the "MYDIR" directory with the following content:
Dockerfile
# Use the official Ubuntu base image
FROM ubuntu
# Set the maintainer label
MAINTAINER Raj
# Update package lists and install Vim
RUN apt-get update && apt-get install vim -y
# Define the default command to run when the container starts
CMD ["/bin/echo", "Hello From Container"]
- This Dockerfile specifies a base image (Ubuntu), sets the maintainer label, updates package lists, installs Vim, and defines the default command to run when the container starts.
3. Build the Docker Image:
Run the following command to build a Docker image using the Dockerfile:
docker build -t ub-image:vim .
- `docker build`: This command builds a Docker image.
- `-t ub-image:vim`: It tags the image with the name "ub-image" and the tag "vim."
- `.`: Specifies the build context, which is the current directory (where the Dockerfile is located).
Example Output: You will see output indicating the steps being executed during the image build process. If successful, you should see a message like "Successfully built <image_id>".
Sending build context to Docker daemon 4.096kB
Step 1/4 : FROM ubuntu
...
Step 4/4 : CMD ["/bin/echo", "Hello From Container"]
...
Successfully built <image_id>
4. Verify the Newly Created Image:
You can list the Docker images to verify that the "ub-image:vim" image has been successfully created:
docker images
Example Output: You will see a list of Docker images, including "ub-image:vim."
REPOSITORY TAG IMAGE ID CREATED SIZE
ub-image vim <image_id> 2 minutes ago <size>
You have successfully created a Docker image named "ub-image" with the tag "vim" using the provided Dockerfile and commands. This image is based on Ubuntu, has Vim installed, and will print "Hello From Container" when run as a container.
Overwrite the CMD and Start an Interactive Shell
1. Run the Docker Container with Default CMD:
docker run -it ub-image:vim
- This command runs a Docker container based on the "ub-image:vim" image without overriding the default `CMD` instruction defined in the image's Dockerfile (which was `/bin/echo "Hello From Container"`). It starts the container with the default command.
Example Output: When you run this command, the container will execute the default `CMD`, which prints "Hello From Container" to the terminal, and you'll see this message.
Hello From Container
2. Overwrite the CMD and Start an Interactive Shell:
docker run -it ub-image:vim /bin/bash
- This command runs a Docker container based on the "ub-image:vim" image and overrides the default `CMD` instruction with the new command `/bin/bash`. This starts an interactive shell (`bash`) within the container.
Example Output: When you run this command, you will be inside an interactive shell within the Docker container, and you can execute commands as if you were on a regular Linux system.
root@container_id:/#
Here, `container_id` represents the unique identifier of your running container.
3. View the History of the Docker Image:
docker history ub-image:vim
- This command displays the history of the "ub-image:vim" Docker image, showing the layers and commands that were used to create the image.
Example Output: When you run this command, you'll see a list of image layers, along with their sizes and the commands that were executed to create them. The output will include information about each layer's creation, such as the `RUN` commands from the Dockerfile.
IMAGE CREATED CREATED BY SIZE COMMENT
abcdef123456 2 hours ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
...
123456789abc 3 hours ago /bin/sh -c apt-get update && apt-get install… 100MB
...
In the output, `abcdef123456` and `123456789abc` are example image IDs, and the `CREATED BY` column shows the commands executed for each layer.
The first command runs the Docker container with the default `CMD`, which prints the specified message. The second command overwrites the `CMD` with `/bin/bash`, allowing you to start an interactive shell within the container. The third command displays the image history, showing the build steps and commands used to create the image layers.
Port Forwarding in Docker
1. Port Forwarding in Docker:
Docker allows you to map ports from your host machine to the container. You can use the `-p` or `-P` option when running a container.
- `-p` allows you to specify a mapping (e.g., `-p 8080:80` maps port 8080 on your host to port 80 in the container).
- `-P` automatically maps all exposed ports in the container to random ports on your host.
2. List Docker Images:
docker images
- This command lists all the Docker images that are available on your local system.
Example Output: Here's an example output listing Docker images:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 0123456789ab 2 weeks ago 72MB
nginx latest abcdef123456 4 weeks ago 132MB
3. Run an Nginx Container:
docker run -itd nginx
- This command runs a detached (`-d`) Nginx container interactively (`-it`), which means it runs in the background and you can interact with it.
4. List Running Containers:
docker ps
- This command lists all the running Docker containers.
5. Inspect a Container:
docker inspect CONTAINER_ID
- This command inspects a specific Docker container and provides detailed information about it, including its configuration and networking details.
Example Output: When you run `docker inspect` on a container, you'll see a JSON-formatted output with a wealth of information about the container. This output can be quite extensive, so you might need to scroll through it to find specific details.
6. Curl the IP of a Container:
After inspecting the container, you can use `curl` or any HTTP client to send requests to the IP address of the container that you obtained from the `docker inspect` output. For example:
curl CONTAINER_IP
Replace `CONTAINER_IP` with the actual IP address you found in the inspect output.
7. Run an Nginx Container with Port Forwarding:
docker run -itd -P nginx
- This command runs a detached Nginx container with automatic port forwarding. Docker will assign random available ports on your host and map them to the exposed ports in the container.
8. List Running Containers with Ports:
docker ps
- This command lists all running Docker containers, including the ports they are forwarding.
9. Inspect a Container for IP Address and Ports:
docker inspect CONTAINER_ID | grep -E -A 3 "IPAddress|Port"
- This command inspects a specific Docker container (`CONTAINER_ID`) and uses `grep` to filter the output to display the IP address and port information, along with some additional context.
Example Output: Here's a simplified example of the output you might see for this command:
"IPAddress": "172.17.0.2",
"PortBindings": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
}
]
}
This output shows the container's IP address and the mapping of port 80 inside the container to a random port (in this case, 32768) on the host.
Specific Port Forwarding
1. Run an Nginx Container with Specific Port Forwarding:
docker run -itd -p 9090:80 nginx
- This command runs a detached (`-d`) Nginx container and maps port 9090 on your host to port 80 in the container.
2. List Running Containers:
docker ps
- This command lists all the running Docker containers.
3. Inspect a Container for IP Address and Ports:
docker inspect CONTAINER_ID | grep -E -A 3 "IPAddress|Port"
- This command inspects a specific Docker container (`CONTAINER_ID`) and uses `grep` to filter the output to display the IP address and port information, along with some additional context.
Example Output: Here's an example of the output you might see after running these commands:
"IPAddress": "172.17.0.2",
"PortBindings": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9090"
}
]
}
This output shows that the container's IP address is "172.17.0.2," and port 80 inside the container is mapped to port 9090 on the host machine. You can access the Nginx web server running in the container by using your host machine's IP address and port 9090 in a web browser.