Create Containers and Interaction in Docker
Tags: docker, containers, webdev, tutorial
Published: 2024-01-17 13:00:00

## 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:**
   ```bash
   docker container create --help
   docker container create hello-world:linux
   ```
   - The `create` command doesn't start the container; it merely generates it.

3. **Listing Containers:**
   ```bash
   docker ps
   docker ps --all
   ```
   - The status "0 exit code (exited)" indicates successful execution.

4. **Starting the Container:**
   ```bash
   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:

1. **Single Command Magic:**
   ```bash
   docker run <image_name>
   ```
   - This command combines create, start, and attach actions.

2. **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:**
   ```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'.

2. **Building an Image:**
   ```bash
   docker build -t <image-name> .
   ```
   - Use `-t` to tag an image.

3. **Intermediary Images:**
   - Docker generates intermediate images for each line in the Dockerfile.
   - The final image is squashed and tagged.

4. **Running the Image:**
   ```bash
   docker run <image-name>
   ```

### Removing Images

To maintain a clean and efficient system, it's crucial to remove unused images:
- To remove an image:
```bash
 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:
  ```bash
  docker run -d --name <container_name> <image_name>
  ```

### Port Binding

- Bind ports for network access:
  ```bash
  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:
  ```bash
  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

1. **Understanding Registries:**
   - Container image registries track and store container images.
   - Docker Hub is the default registry.

2. **Pushing Images to Docker Hub:**
   ```bash
   docker login
   docker tag <image_to_rename> username/new_image_name
   docker push <image-name>
   ```

3. **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:**
- [Docker Documentation. Docker run reference.](https://docs.docker.com/engine/reference/run/)
- [Docker Documentation. Dockerfile reference.](https://docs.docker.com/engine/reference/builder/)
- [Docker Documentation. Docker CLI documentation.](https://docs.docker.com/engine/reference/commandline/cli/)
- [Docker Documentation. Manage data in containers.](https://docs.docker.com/storage/)
- [Docker Documentation. Docker Compose overview.](https://docs.docker.com/compose/compose-file/)