Use Docker to build your development environment

Source: Internet
Author: User
Tags docker run

As a freelancer, often can receive a lot of development work, these gold master, have like PHP, have habit to wash, have preference SQLite, also have like PG, I even saw MySQL, pg use project together, under the same language, the use of the framework is different, Useful CI, also useful Yaf.

In peacetime development, I have a 10 (or 11) ThinkPad E50, at home as a Linux server, there is a RMBP to do development and display. For the general development work, their two environments are similar, but (right in the game, "but" before, are put P), in some special scenarios, their differences are still very uncomfortable (such as Ubuntu under the Yaf and OS X is not the same; in the coffee find the path also has Subtle differences, especially after brew follows the OS upgrade ...). These differences are small, but on a project, it took me some time to build the environment.

Recently studied Docker, and then found that Docker can be a perfect solution in the development due to the host differences caused by the environmental problems (online environment I have not tried, in fact, I do not have an online environment), below I demonstrated on the Linux host, starting from 0 to build a development environment needs Docker The process of mirroring and everywhere.

Docker installation

Follow https://docs.docker.com/'s introduction to install. Of course, if you're using Ubuntu, it's good to execute the following command:

sudo apt-get install Docker.io

Pull a docker image from the network

Ignore the various tutorials you see online, and use the following command directly (if you are not the root user, don't forget to add sudo)

Docker pull Ubuntu

After a long or short period of time, you will pull an Ubuntu image from the Docker to your disk. You can use

Docker images

See it, like this:

Start the downloaded image and go to the container where the image is running

Use the Run command to start the corresponding image

Docker RUN-ITD Ubuntu

Then use the PS command to locate the container ID that was just started, and in the use of the Attach command, enter the container (note that the attach parameter is the ID of the container listed in PS)

Software required to install the development environment

The specifics of this step, depending on what development environment you need, I usually install Nginx, PHP5-FPM, MySQL, MongoDB, redis, and Python environments, using Apt-get installation (don't forget to apt-get update first). Of course, there will be some other operations, such as Nginx configuration, these peace when the installation service on the Liunx is the same.

After you install the environment, do not worry about stop off the corresponding container.

Container to save changes

After modifying the container in the previous step, if you turn off the container directly, the changes will not be retained at the next boot, so it needs to be stored in a different local mirror.

method is very simple, also need to attach that step to get the ID

Docker commit 028ce7fa5596 Ptj:dev

I named my local Mirror "Ptj:dev", where you can get a name.

Now you can safely stop the front container:

Docker Stop 028ce7fa5596

Then we'll use the images command to look at the Docker image:

Ptj:dev is now on our disk.

Increase boot service, port mapping

Now "Ptj:dev" has the software we need to develop, but does not allow Nginx, MySQL these services automatically start, the following need to put the service in the Ptj:dev, with the image to start.

The concrete steps are tedious, but they are not difficult.

1. Create a new directory and we'll call it PTJ

mkdir PTJ; CD PTJ

2. In the directory, create a new two files

Touch Dockerfile start.sh

Dockerfile tells Docker how to make an image, Start.sh is our own startup script.

3. Write the services that need to be started to start.sh, then add "/bin/bash" at the end

#!/bin/bash
Service Nginx Start
Service MySQL Start
Service PHP5-FPM Start
Service MongoDB Start
Service Redis-server Start
/bin/bash # Don't miss the last line

4. Copy the image from Ptj:dev and put the start.sh into the mirror to start automatically

From Ptj:dev
ADD start.sh/usr/local/start.sh
RUN chmod +x/usr/local/start.sh
EXPOSE 80 22 3306 6379 27017
CMD ["/usr/local/start.sh"]

From that line, which represents the image copied from

Add means put the file in the mirror
RUN needs to turn start.sh into executable

EXPOSE is to expose the port within the Docker to the host and in the future can access the services in Docker via-p

CMD is a program that indicates that the image needs to be executed

5. Create a new image

In the PTJ directory in front of us, execute an instruction:

Docker build-t PTJ.

Start a new image to check if the service is working properly

The images command is again sacrificed:

Can find PTJ below a latest, this is the image we have just modified;

Use the run, attach described above to see how our services are running:

You can see that our service has all been running with the start of mirroring.

The rest is to export Docker into a tar package, copy it between different machines, import it, and then develop it.

Use Docker to build your development environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.