Create Containers and Interaction in Docker
Ramkumar Jayakumar
Posted on January 17, 2024
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'
-
Understanding Images:
- Images are compressed file systems with instructions on how to start your app.
- Docker Hub serves as a default repository for images.
Command to Create Container:
docker container create --help
docker container create hello-world:linux
- The
create
command doesn't start the container; it merely generates it.
- Listing Containers:
docker ps
docker ps --all
- The status "0 exit code (exited)" indicates successful execution.
- Starting the Container:
docker container start <container_id>
- 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:
- Single Command Magic:
docker run <image_name>
- This command combines create, start, and attach actions.
-
Note:
- Use
docker ps
to list containers and retrieve IDs.
- Use
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:
- 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"]
-
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'.
-
- Building an Image:
docker build -t <image-name> .
- Use
-t
to tag an image.
-
Intermediary Images:
- Docker generates intermediate images for each line in the Dockerfile.
- The final image is squashed and tagged.
Running the Image:
docker run <image-name>
Removing Images
To maintain a clean and efficient system, it's crucial to remove unused images:
- To remove an image:
docker rmi <image_name>
- 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>
Port Binding
- Bind ports for network access:
docker run -d --name <server_name> -p 5001:5000 <image_name>
- Access the server at http://localhost:5001.
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"
- Map a folder on your machine to a folder in the container.
Container Registries
-
Understanding Registries:
- Container image registries track and store container images.
- Docker Hub is the default registry.
Pushing Images to Docker Hub:
docker login
docker tag <image_to_rename> username/new_image_name
docker push <image-name>
-
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:
Posted on January 17, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.