hello everyone my name is josh cummingsand today i'm going to talk about docker and wordpress. a little bit about myself,i'm a developer at grizzly. we're a full-service creative agency in sandiego. we focus on anything from web design and development to brand, strategy,identity, and all sorts of fun creative projects for awesome companies. so, docker.what is it, why would you use it, and how can it make wordpress development better? well, before we can really answer any ofthese questions, we need to take a look at why docker. exists in the first place. soit used to be that we would write our code and deploy it to a server and thatserver was most likely, in the beginning
phases of our development, some sort ofblack box that was just hosted somewhere and we were able to push your code to itand it would all just magically work. when you take a deeper look at the server, yourealize that there's a lot of dependencies that live on that serverand each one of those dependencies has their own version. now, a lot of timesyou're not even just dealing with one server you're dealing with three or moreif you're doing your local or development server one place you'll haveanother staging environment and then a production environment etc etc and allof those server environments have their own dependencies on them and usually wecontrol this right so we'll have a local
in a staging server that we set up andwe control we make sure it's running properly but let's say your productionserver is run by the client and they're using let's say a different version oflinux than what's on our staging server well then we go to deploy, it might workon staging, but we might run into issues on on production that really get in the way of our workflow and slow us down and cause problems and when we are troubleshooting let's say databaseerrors or php errors that can really take time away from what we're doing andmake coding very very frustrating and the deployment process really really
frustrating. so, how do we fix this? oneway of fixing this is configuration management because with configurationmanagement you write one file that lives in your code base that tells the serverwhat dependencies to install and how to sort of configure itself so you may haveheard of vagrant or ansible or some of these configuration management tools the problem with these tools is thatthey run on virtual machines and virtual machines can be very expensive meaningthey use a lot of cpu, memory, take up a lot of storage on your system,and these virtual machines run on guest operating systems that can't really talkto each other. so what we really need are
applications that can talk to each otherthat share the same os kernel. so, meet the container the container is a reallya complete file system that is created with all your dependencies of all of thetools that you need to run that can live in an image somewhere and then thatimage can be run anywhere we go on any environment. so, if you take a look atthis picture you'll see we have a busybox image and a debian image and thoseare both living in their separate containers but they can still talk toeach other because they're sharing the same os kernel. so, what is an image? theway that i like to think about images is the same way i think about images insomething like digitalocean. so if you
wanted to spin up a new server indigitalocean you would choose your ubuntu 16.04 image and then that imagewould spin up onto a server. it's the same way with containers. you create animage with all your configuration in it and then that image can run inside of acontainer. going the other way, because we're running a full network stackinside of a container, we need to actually expose port back to our host so so for example if i'm working on mylocal machine and i spin up a container that container might have port 80exposed inside of it but i'm not going to be able to get to it from my browserunless i expose port 80 outside of the
container to my host and we'll take alook at how what this means in just a minute going back in the other way, or ratherboth ways, are volumes. now, if i want to write code on my local machine thatinteracts with my container i can actually mount of volume and that volumewill allow me to work locally on my machine and make changes inside thecontainer so it sort of takes one of your directories directories and mountsthat as a volume inside your container will also take a look at that docker hubis similar to github where you can store all of your images and access themfrom one repository that lives in the
cloud and so workflow for this for dockermight look like you pulling a wordpress image from docker hub and thenrunning it inside a container on your local environment or staging or developmentand production or wherever. the cool thing about this, is it's guaranteed towork on any environment as long as the docker engine is running. so this is reallysolving that problem of not having a server parity and just really makingsure you're running the exact same application everywhere you go and thatincludes the dependencies, the network stack, the operating system, everything isgoing to be the same because docker is controlling that for us
you can also integrate this into acontinuous integration workflow so what you're seeing here isus pushing to github, docker is seeing that push and then startinga container based on that push inside of each one of ourdevelopment environments. it's a bit more complicated than that, but just to give you a high-level overview you can actually trigger pushes to docker huband pulls and all of that through a continuous integration workflow and thatmakes for a really really nice workflow where you're already pushing code to githubyou might as well have it spin up some containers for you and deployautomatically. alright, so let's take a
look at some examples. the first example i'm going to do is dockerrun. docker run tells docker to run a container from an image and the image i'm going to use is alpine. alpine is a really small linux distribution it'sabout five megabytes in size so we're going to run that container from thealpine image and we're going to run the command ping and i'm just gonna ping mylocalhost let's say five times. when i press enter here, it's going to grab analpine image from the repository start a container and run the ping commandinside the container. so let's see that happen. so it can't find it locally soit's going to pull it down from the repo
and there's my ping command five times.so let's try another example. let's say docker run and this time i'm going to rundebian and let's say we actually want to get command line access inside of debianand run commands that way i'm all you have to do is pass the -it flag and thenthe the shell that you want to use so i'm going to use just a regular shell. soyou'll see that it doesn't have debian installed locally, but you'll also seehere that it says already exists. that's because debian and alpine bothuse busybox so it already had busybox installed from the first alpineinstallation it doesn't need to install that again. it caches it locally. so, here iam inside of the shell inside of debian
and i can run something as simple asecho "hello world" and there we go! so, two different operatingsystems completely running locally on my mac and i didn't have to do anything butinstall docker and run a couple simple commands. pretty sweet! so,i'm gonna go ahead and exit that and now let's go ahead and try to get wordpressrunning. so if we go to the official repository for both wordpress and mariadb, we can start to piece together how to run these containers. i'm going to usemariadb and actually link it to wordpress so that we can use wordpresswith mariadb here using docker. so, first let's start at mariadb the thecommand is sort of intimidating-looking
but don't don't fret, i will walk youthrough how it works so the command looks something like this.we're doing a doctor run here and we're giving our container a name i'm actually just going to call it...youcan call it whatever you want, i'll just say coolmysql we're passing anenvironment variable here with this -e flag and we're just going to use "wp" asthe mysql root password. the -d flag tells it to run in detached so it'llactually run in the background and the last argument here is the image that iwant to use and i'm actually going to use the exact tag here. so, i want to use...10.1.17 is the version i want to
use so let's add that. and then thelast thing we'll need to do is expose a port. so let's go ahead and expose port... let's map port 8081 to 3306 inside thecontainer and again remember because mysql is running inside the container, weneed a way to access that container through a port. so i'm justexposing an arbitrary port here (8081) to get inside of that container. so, let's goahead and run this. and you'll see how lightning fast that was, and if i run docker images, it's becausei i already have mariadb installed fromanother project i was working on and
we'll see i already have wordpressinstalled from another project i was working on. so, you can start to see asyou get these containers running its really really fast to spin up new newcontainers from these images that you already have so, if we run docker ps, we can see ourrunning containers and i can see that mariadb is running on port 8081 on mylocalhost its mapping to port 3306 in my container. that all looks good! so let'sgo ahead and link wordpress to it. so going back to wordpress in docker hub,we can see how to use this image. we'll grab this command here paste it on in.and let's just modify it slightly. so we
have docker run, we're going to create aname for our wordpress container. let's just call it coolwordpress (again thatcan be anything you want just to remember which container is which). we're going to link this to our other container we haverunning which is called coolmysql again we're running it in detached andwe're going to expose port 8080, we're going to basically map port 80 insidethe container to 8080 on my localhost. now, we're going again use an exact versionhere so i'm going to be using 4.6.1-apache so let's type that in. and then the last thing we need to do if you remember we set a mysql root password. we need to actually
pass that environment variable here towordpress and the environment variable we use is called wordpress_db_password and we calledthat "wp" so we'll keep it the same. there we go! and that's all running. so if i do docker ps again, you can seethat both of these containers are running. wordpress is on port 8080 and mysql ison port 8081. so now if i go to my localhost port 8080...tada! there's ourwordpress install running inside these containers. i didn't have to installanything. i didn't have to do anything but run these commands inside of dockerand docker is taking care of all of that for me
now you might be thinking to yourself "doi really have to run these crazy commands every single time i want tospin up wordpress?" and the answer is no docker has a solution for this called dockercompose. so if we head on back to the official wordpress repositoryon docker hub, we can see an example docker compose file. now gettingback to our configuration management, we want to have a file that sits in ourcode base that allows us to spin up the containers exactly with that we wantusing configuration management best practices. so i've actually created asample docker-compose.yml file, so let's just open that up.
so, what's happening here is prettymuch exactly the same as what was happening in the commands that we ran. soinstead of running a big long string of commands in the command line, let's justwrite one docker-composed.yml file that does the exact same thing. so youcan see i'm grabbing the images for each of these containers i'm exposing ports, i'm linking and i'msetting environment variables, all the same stuff we just looked at you'll see this volumes section whichis new. so what this is saying is in my current directory mount a folder calledwp-content to the wp-content folder
inside my container. so this is how i'mable to work locally on my machine but still have changes be made inside mycontainer, and you can see i'm doing the same thing here with mysql. so, this all looks good i'm just going tochange the ports here so we don't have any conflicts. so i'm going to say runthis on let's just say 82 and 83 (why not) so now all we have to do is run fromthis directory docker-compose up and i'm going to pass it the -d flag to againrun as a daemon here in the background. so you'll see a couple things happen itcreated the containers so if i say docker ps
i can see now that it spun up a couplenew containers for me that are running on those ports i specified, and then youcan also see inside my project it "automagically" pulled in the wp-contentfolder and mysql folder for me so i can go ahead and start working on myproject. so if i jump back over and go to port 8082 on my localhost..tada! there's mywordpress installation! this is the way that i love to work, and i hope you'reseeing the value of docker and how great this is. so, as you can see once we havethese docker-compose.yml files, our speed of development is going to increasebecause we're able to start up as many containers as we want using thesefiles and we don't have to worry about
dependencies, they're all managed for uswith docker. and i can run these containers on any server environment. aslong as a docker engie is running, it's guaranteed to work. it's veryeasy and it's also easy to get new team members on board because you don'thave to worry about having the same configuration as long as they have dockeron their machine and you have docker on yours, you're on the same page, you cansay here you go here's the project get going, and it's also extremely scalablebecause you're able to use tools like docker machine and docker swarm to scale theseapplications and use other tools to load balance them, so it's it's very scalableand you're able to start small and build
as your application grows. if you want to learn moreabout docker, docker has really great self-paced training. it's three videos asof now and they're really in-depth and really great for learning docker, andthere's also some other great resources that i will post in the link below. alright everyone thanks so much! i hopeyou really got something out of this video and you're starting to see how touse docker in your workflow. if you have a workflow that you really like to use,please share it. and as always, i'm here to answer questions and have a great one!
happy coding!
Tidak ada komentar:
Posting Komentar