This Docker process outlines how to set up a Docker container on an EC2 instance, build a Docker
image from a custom Dockerfile, and run a container from that image. Here's a step-by-step
explanation of the process:
### 1. **Launch an EC2 instance**
- Start an Amazon EC2 instance using AWS. This instance will act as the host machine where Docker
will be installed.
### 2. **Update the EC2 instance**
```bash
yum update
```
- This command updates the packages and software on the instance to their latest versions.
### 3. **Install Docker**
```bash
yum install docker -y
```
- Install Docker on the EC2 instance using the package manager `yum`. The `-y` flag automatically
answers "yes" to the installation prompts.
### 4. **Start the Docker service**
```bash
systemctl start docker
```
- This starts the Docker service on the EC2 instance so you can begin using Docker commands.
### 5. **Pull the Ubuntu image**
```bash
docker pull ubuntu
```
- This command pulls the latest Ubuntu image from Docker Hub (the default Docker registry).
Ubuntu will be the base image for your custom Docker image.
### 6. **Run a Docker container**
```bash
docker run -it --name abil ubuntu /bin/bash
```
- The `docker run` command launches a new container from the Ubuntu image.
- `-it` allows you to interact with the container via a terminal.
- `--name abil` assigns the name "abil" to this container.
- `/bin/bash` opens a Bash shell inside the container, allowing you to run commands within it.
### 7. **Run basic commands inside the container**
```bash
ls
```
- Lists the files in the current directory inside the container.
```bash
exit
```
- Exits the running container and returns to the EC2 instance shell.
### 8. **Create a Dockerfile**
```bash
vi Dockerfile
```
- This opens the `vi` editor to create a new file named `Dockerfile`. A Dockerfile is a script
containing instructions on how to build a Docker image.
### 9. **Write the Dockerfile content**
```
FROM ubuntu
WORKDIR /data
RUN echo "Abil ki Dir" > /test
ENV myname abil
COPY newfile /data
ADD test.tar.gz /data
```
- **FROM ubuntu**: Specifies that the base image for this Dockerfile is Ubuntu.
- **WORKDIR /data**: Sets the working directory inside the container to `/data`. If the directory
doesn't exist, it is created.
- **RUN echo "Abil ki Dir" > /test**: Creates a file named `/test` and writes the string "Abil ki Dir"
into it.
- **ENV myname abil**: Sets an environment variable `myname` to "abil".
- **COPY newfile /data**: Copies a file named `newfile` from the EC2 instance's current directory
to the `/data` directory inside the image.
- **ADD test.tar.gz /data**: Adds (extracts) a tarball `test.tar.gz` from the EC2 instance to the
`/data` directory in the image.
### 10. **Save and exit `vi`**
- After writing the Dockerfile, save it and exit `vi` by typing `:wq`.
### 11. **Create additional files**
```bash
touch test
tar -cvf test.tar test
gzip test.tar
touch newfile
```
- **touch test**: Creates an empty file named `test`.
- **tar -cvf test.tar test**: Archives the `test` file into a `test.tar` tarball.
- **gzip test.tar**: Compresses the tarball into `test.tar.gz`.
- **touch newfile**: Creates an empty file named `newfile`.
### 12. **Build the Docker image**
```bash
docker build -t dell .
```
- This command builds a Docker image from the `Dockerfile` located in the current directory (`.`).
- `-t dell`: Tags the image with the name "dell" so that it can be easily referenced later.
### 13. **Run the Docker container from the image**
```bash
docker run -it --name container dell /bin/bash
```
- This launches a new container from the "dell" image, giving it the name "container".
- `-it` allows interaction with the container via a terminal.
- `/bin/bash` opens a Bash shell in the container.
### 14. **List files inside the container**
```bash
ls
```
- This command will show files inside the `/data` directory:
- `test.tar.gz`: The compressed tarball.
- Extracted contents of the tarball, such as the `test` file.
- `newfile`: The empty file created earlier.
### Outcome:
- The tarball (`test.tar.gz`) was successfully added to the image and should now be available inside
the `/data` directory.
- You will also find the `newfile` that was copied using the `COPY` command from the Dockerfile.
### Summary:
- You created a Docker image based on Ubuntu, customized it by adding files (`newfile`, `test.tar.gz`),
and ran a container from the built image. The final container has both the compressed tarball
(`test.tar.gz`) and the extracted files from it.