Infrastructure
Create and Run Image
Dockerfile

Before creating a Docker image for Ubuntu, you need to have Docker installed on your system.

  • Docker :: Ensure Docker is installed and running on your machine. If you haven’t installed Docker yet, refer to Lesson 2 for installation steps.
1. Create a Dockerfile

A Dockerfile is a script that contains a series of commands to build a Docker image.

Set Up Your Project Directory:

Create a new directory for your Docker project and navigate to it:

  mkdir my-ubuntu-image
  cd my-ubuntu-image
Create a Dockerfile:

Inside the project directory, create a file named Dockerfile with the following content. You can use VS Code or an text editors like vi or nano:

To create and open the Dockerfile in VS Code , use:

touch Dockerfile && code Dockerfile

This command will create the Dockerfile and open it in VS Code for editing.

Next, add the following content to the Dockerfile:

 # Use the official Ubuntu base image

FROM ubuntu ## This line specifies the base image for the Docker container. In this case, 
                #it's the official Ubuntu image.# Set environment variables (optional) 

ENV DEBIAN_FRONTEND=noninteractive ## This sets an environment variable to make apt-get commands run non-interactively,
                                          #avoiding prompts during package installations.
                                          # Update the package list and install basic packages  

RUN apt-get update && apt-get install -y \  

curl \ ## Installs curl, a command-line tool for transferring data with URLs.

vim \ ## Installs vim, a text editor.

git \ ## Installs git, a version control system. 

&& rm -rf /var/lib/apt/lists/* ## Removes the package list to reduce image size.# Set the working directory  

WORKDIR /root ## Sets the working directory inside the container to /root.
                # Copy files from the host to the container (optional)

COPY . /root ## Copies all files from the current directory on the host machine to the 
                #/root directory inside the container.# Define the command to run when the container starts

CMD ['bash'] ## Specifies that the default command to run when the container starts is bash, 
             #which opens a terminal session.  
2. Build the Docker Image
Build the Image from the Dockerfile:

Use the docker build command to create a Docker image from your Dockerfile:

The Dockerfile is in the Current Directory:

docker build -t my-ubuntu-image .
  • The -t my-ubuntu-image flag tags the image with the name my-ubuntu-image.
  • The . at the end of the command tells Docker to look for the Dockerfile in the current directory.

If the Dockerfile is in a different directory, you can specify the path to that directory and the Dockerfile. For example, if your Dockerfile is in a directory named dockerfiles, you would use the following command:

docker build -t my-ubuntu-image -f /path/to/dockerfiles/Dockerfile /path/to/dockerfiles

  • -t my-ubuntu-image: Tags the image with the name 'my-ubuntu-image.'
  • -f /path/to/dockerfiles/Dockerfile: Specifies the path to the Dockerfile if it's not named Dockerfile or located in the default context directory.
  • /path/to/dockerfiles: Specifies the build context, which is the directory where the Dockerfile is located.
Verify the Image Creation:

List all Docker images on your system to verify that your image was created successfully:

docker images

You should see my-ubuntu-image listed among the images.

3. Run the Docker Container
Run the Container from the Image:

Use the docker run command to create and start a container from your image:

 docker run -it --name my-ubuntu-container my-ubuntu-image
  • The -it flag starts the container in interactive mode, allowing you to interact with it via the terminal.
  • The --name my-ubuntu-container option names the container my-ubuntu-container.
  • my-ubuntu-image specifies the image from which to create the container.
Verify the Container is Running:

In a new terminal window, you can check the list of running containers using:

 docker ps

Your container should be listed, showing it is up and running.

Exit the Container:

To stop and exit the container, type exit or press Ctrl + D in the container's terminal.

Have a doubt?
Post it here, our mentors will help you out.