DEV Community

Cover image for Create Containers and Interaction in Docker
Ramkumar Jayakumar
Ramkumar Jayakumar

Posted on

Create Containers and Interaction in Docker

Building Containers: The Detailed Way

Containers in Docker are born from container images—a compressed, pre-packaged file system containing your app, its environment, and configuration instructions. Let's embark on the journey of container creation, starting with the meticulous approach.

Create Container Using 'docker container create'

  1. Understanding Images:

    • Images are compressed file systems with instructions on how to start your app.
    • Docker Hub serves as a default repository for images.
  2. Command to Create Container:

   docker container create --help
   docker container create hello-world:linux
Enter fullscreen mode Exit fullscreen mode
  • The create command doesn't start the container; it merely generates it.
  1. Listing Containers:
   docker ps
   docker ps --all
Enter fullscreen mode Exit fullscreen mode
  • The status "0 exit code (exited)" indicates successful execution.
  1. Starting the Container:
   docker container start <container_id>
Enter fullscreen mode Exit fullscreen mode
  • Check logs using docker logs <container_id>.

Choosing the detailed way may be preferable in scenarios where you want granular control over container creation, particularly when specific configurations or setups are required.

Shortcut: 'docker run' Command

On the flip side, the 'docker run' command offers a more efficient and convenient way to create and run containers:

  1. Single Command Magic:
   docker run <image_name>
Enter fullscreen mode Exit fullscreen mode
  • This command combines create, start, and attach actions.
  1. Note:
    • Use docker ps to list containers and retrieve IDs.

Experimenting with both methods is encouraged. The 'docker run' command is often favored for its simplicity, making it an excellent choice for quick development iterations.

Dockerfile and Image Creation

Dockerfile Basics

Introducing Dockerfile basics is essential for efficient image creation. Let's explain each component succinctly:

  1. Example Dockerfile:
   FROM ubuntu:latest
   LABEL maintainer="Your Name"
   USER nobody
   COPY . /app
   RUN apt-get update && apt-get install -y curl bash
   USER nobody
   ENTRYPOINT ["curl"]
Enter fullscreen mode Exit fullscreen mode
  • Explanation:
    • FROM ubuntu:latest: Specifies the base image as the latest version of Ubuntu.
    • LABEL maintainer="Your Name": Adds metadata to the image, specifying the maintainer.
    • USER nobody: Sets the default user for subsequent commands to 'nobody'.
    • COPY . /app: Copies the contents of the current directory to the '/app' directory in the image.
    • RUN apt-get update && apt-get install -y curl bash: Updates package lists and installs 'curl' and 'bash'.
    • USER nobody: Switches back to the 'nobody' user.
    • ENTRYPOINT ["curl"]: Defines the default executable for the container as 'curl'.
  1. Building an Image:
   docker build -t <image-name> .
Enter fullscreen mode Exit fullscreen mode
  • Use -t to tag an image.
  1. Intermediary Images:

    • Docker generates intermediate images for each line in the Dockerfile.
    • The final image is squashed and tagged.
  2. Running the Image:

   docker run <image-name>
Enter fullscreen mode Exit fullscreen mode

Removing Images

To maintain a clean and efficient system, it's crucial to remove unused images:

  • To remove an image:
 docker rmi <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Use docker images to list images.

Advanced Container Operations

Container Naming

Organizing advanced container operations under subheadings for better structure:

  • Assign a name to your container:
  docker run -d --name <container_name> <image_name>
Enter fullscreen mode Exit fullscreen mode

Port Binding

  • Bind ports for network access:
  docker run -d --name <server_name> -p 5001:5000 <image_name>
Enter fullscreen mode Exit fullscreen mode

Volume Mounting

  • Preserve data outside containers using volumes:
  docker run --rm --entrypoint sh -v /tmp/container:/tmp ubuntu -c "echo 'Hello There.' > /tmp/file && cat /tmp/file"
Enter fullscreen mode Exit fullscreen mode
  • Map a folder on your machine to a folder in the container.

Container Registries

  1. Understanding Registries:

    • Container image registries track and store container images.
    • Docker Hub is the default registry.
  2. Pushing Images to Docker Hub:

   docker login
   docker tag <image_to_rename> username/new_image_name
   docker push <image-name>
Enter fullscreen mode Exit fullscreen mode
  1. Deleting Images in Docker Hub:
    • Delete images through the Docker Hub settings in the browser.

Conclusion

Mastering container creation and interaction forms the backbone of Docker. Whether you choose the detailed or shortcut approach, understanding the nuances empowers you to efficiently manage and deploy your applications.

References:

Top comments (0)