banner
soapffz

soapffz

github
steam
bilibili
douban

Install Docker in Ubuntu to quickly set up a testing environment.

Why Use Docker?#

In practical learning, it is often necessary to simulate different vulnerability environments. However, using instances from the public network carries some risks. Therefore, setting up a local simulated environment to test vulnerabilities is a good solution. Docker is a popular open-source container engine in recent years, and many beginner-friendly vulnerable machine environments have been built using Docker containers.

Reason for writing this article: I have always known the power of Docker, but I needed an example to get started.

These two articles from a public account:

were the motivation for me to install Docker.

Why Ubuntu?#

Our common testing environments include Win10, Kali, Win7, Win2008, Win2003, etc. Another suitable Linux environment is Ubuntu.

The most commonly used environment for most people is Win10 (sorry, MacOS users!). So, how about installing Docker on Win10?

According to the official Docker for Windows documentation: https://docs.docker.com/docker-for-windows/install/

Docker for Windows is not supported on Win10 Home Edition. For other versions of Win10, such as Professional Edition (which I personally use), you need to enable the Virtualization feature in the BIOS. I have tried many times, but Docker for Windows does not work when Hyper-V is enabled. It gives an error message saying "Cannot run software in a virtual environment" when connecting to the campus network.

For machines that do not support the installation environment, you can only use Docker Toolbox. Documentation: https://docs.docker.com/toolbox/toolbox_install_windows/

Docker Toolbox needs to be used with VirtualBox. So, why did I choose to install Docker on Ubuntu?

  • I needed a lightweight Linux target machine.
  • Virtual machines can be frequently restored from snapshots (although the existence of Docker greatly reduces the complexity of snapshots).
  • I just wanted to use it! (Proud face)

Installing Docker#

Installation#

Since I am lazy, I switched to the root account directly using "su root" to execute the commands. So, if the commands do not work, add "sudo" in front of them.

  • Install the required packages (assuming your Ubuntu has already been configured with a domestic apt mirror): apt install docker.io
  • Check the version information after installation: docker -v
  • Add permissions (optional): Add the current user to the docker group so that you can run Docker without using sudo (you need to log in again): sudo usermod -aG docker $USER

Configuring Image Acceleration#

Similar to Git, the default Docker images are hosted on servers abroad, so the download speed is slow. Therefore, it is necessary to switch to domestic mirrors.

The configuration file for the latest version of Docker is /etc/docker/daemon.json, which does not exist by default after Docker is installed. It will be automatically created when you open it with vim.

Open it with vim: vim /etc/docker/daemon.json, and paste the following code:

{
  "registry-mirrors": ["http://example.m.daocloud.io"]
}

Here are some recommended mirror addresses:

![][1]

  • Then, you will be redirected to an advertisement page: https://www.daocloud.io/mirror. It looks like an advertisement, but it has actually generated a unique accelerator address for us. Scroll down to find the configuration tutorial:

![][2]

The blurred area is the accelerator address.

Restart the Docker service: service docker restart. After that, you can test it by pulling a WordPress image: docker pull wordpress.

Setting Up a WordPress Test Environment#

You can use the official Docker image: docker pull wpscanteam/vulnerablewordpress

![][3]

After pulling the image, run it and forward ports 80 and 3306.

docker run --name vulnerablewordpress -d -p 80:80 -p 3306:3306 wpscanteam/vulnerablewordpress

![][4]

Open the IP address in your browser to access the WordPress installation page:

![][5]

Alternatively, you can set up a WordPress and MySQL 5.6 environment to learn some commands. Here is the tutorial:

After configuring the mirror address, pull the WordPress image docker pull wordpress and the MySQL 5.6 image docker pull mysql:5.6.

Pulling images:

![][6]

Create a MySQL container:

docker run --name testmysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=a123456 -d mysql:5.6
# Parameter explanation:
# MYSQL_ROOT_PASSWORD=a123456 sets the root password of MySQL to a123456
# testmysql sets the container name as testmysql, which will be used later. After it is created, an ID will be returned.

![][7]

Log in to MySQL and create a database:

docker exec -it testmysql mysql -uroot -p
# testmysql here is the name of the MySQL container

Enter the root password set earlier (a123456) to log in to the database:

![][8]

Create a database:

create database wordpress;

Press Ctrl+D or enter quit; to exit.

![][9]

Create a WordPress container, similar to creating a MySQL container:

docker run --name testwordpress --link testmysql:db -p 80:80 -d wordpress:latest
# Parameter explanation:
# 80:80 maps the container's port 80 to the host's port 80
# testmysql:db connects the WordPress container to the MySQL container and sets an alias as db

![][10]

Open the IP address of the host in your browser, and you will be redirected to the WordPress installation page:

![][11]

  • Username: root (MySQL root user)
  • Password: a123456 (Remember the root password you set for MySQL?)
  • Database Host: db (Remember the alias you set earlier?)

![][12]

Leave the other fields as default, and the installation will be completed:

![][13]

A penetration testing environment has been set up.

Isn't it more convenient to download the WordPress image and set it up using VirtualBox, as mentioned in the beginning of the article?

Common Docker Commands#

  • docker version: Check the version number
  • docker run hello-world: Load a test image for testing
  • docker images: View the currently available images
  • docker ps: View all running containers
  • docker ps -a: View all containers
  • docker stop containerId (container ID): Stop the container with ID xxx
  • docker rm containerId (container ID): Delete the container with ID xxx
  • Image deletion:
  • Stop all containers first in order to delete the images: docker stop $(docker ps -a -q)
  • If you want to delete all containers, add another command: docker rm $(docker ps -a -q)
  • Delete images by specifying their IDs: docker rmi
  • To delete untagged images (images with ID ), use: docker rmi $(docker images | grep "^" | awk "{print $3}")
  • To delete all images: docker rmi $(docker images -q)
  • Set container port mapping: docker run [-P][-p]
  • -P: Map all container ports
  • -p: Specify which container ports to map
  • Examples:
  • Only specify the container port, and the host port will be mapped randomly: docker run -p 80 -i -t /bin/bash
  • Specify both the host port and the container port: docker run -p 8080:80 -i -t /bin/bash0
  • Specify the IP and the container port: docker run -p 0.0.0.0:80 -i -t /bin/bash
  • Specify both the IP, the host port, and the container port: docker run -p 0.0.0.0:8080:80 -i -t /bin/bash
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.